Hello!
In this tutorial post we will be learning about two extremely important concepts: inheritance and scope. I know for all of these tutorials I am saying everything is extremely important. Well truthfully everything is, but there are two parts of each concept we have learned. The idea behind the concept and the code and syntax of the concept. It is impossible to memorize all the syntax of code which is why there are tutorials and books like this to refer back to. So don't feel pressured to become experts at Objective-C's concepts immediately. It will take time, and we will be here forever if you need a refresher course.
In this tutorial post we will be learning about two extremely important concepts: inheritance and scope. I know for all of these tutorials I am saying everything is extremely important. Well truthfully everything is, but there are two parts of each concept we have learned. The idea behind the concept and the code and syntax of the concept. It is impossible to memorize all the syntax of code which is why there are tutorials and books like this to refer back to. So don't feel pressured to become experts at Objective-C's concepts immediately. It will take time, and we will be here forever if you need a refresher course.
For this tutorial you will need a starter project.
Inheritance
Open up the starter project. Navigate to the class called SuperClass. Open up the .h file. Look at this line.
What this line means is that the SuperClass class subclasses NSObject. What subclasses means is that SuperClass inherits all of the methods of the class it subclasses. The class that SuperClass subclasses is called a super class. Now you might think that it is confusing that I named the subclass, SuperClass. Well, right now it is, but it will make perfect sense later. Create another class.
Now go to the ViewController.m file. At the top write your import statement for your new class.
Then create an object of SubClass and call the logOutSomething method.
But wait! You never wrote a logOutSomething method in your class of SubClass! Well since it inherits all of the methods from SuperClass. And since SuperClass subclasses NSObject, SubClass inherits NSObject also!
Next let's find out how powerful this actually is! In your SubClass.m file write a method that is called logOutSomething like this.
Now run the code. It should like this in the output area.
What we just did was override a method. We took a method in a superclass and then used the same name and everything but made it do something different. Now say we were to subclass SubClass, and then we called logOutSomething. Well it would output "Logging out something. " This is because since we overrode the method from that class on no matter how many times we subclass that class, that will be what the method will do.
Scope
Now declare another method called scopeMethod in Subclass.m. This is how the .m file should look like.
and the .h file
Inside scopeMethod declare a variable of type int with a value of 0. Call it myInt and log it.
Now go to the init method. Just type myI then stop. Watch the code completion. myInt does not come up right? This is because of scope. myInt is local to scopeMethod. You cannot access its value in any other method or any part of that file. Now comment out the line where you declare the variable. Go to the .h file and declare a variable called myInt of type int.
Go back to the .m file, and go to the init method. Write this.
Run the code it should say myInt = 10. It does! If you tried to access myInt in any other method you could! When you declare a variable it is local to that set of curly braces. If you try to access it outside of those curly braces then it won't work unless you declare it in the .h file like we did. This also applies to loops, if statements, and switch statements.
Wow! That was a lot! Feel free to leave a question, comment, or suggestion below, or email me at [email protected]. Next up in the series is polymorphism which sounds way more complicated than what it actually is.
Thank you,
Zachary Cmiel
The PoKoBros
Wow! That was a lot! Feel free to leave a question, comment, or suggestion below, or email me at [email protected]. Next up in the series is polymorphism which sounds way more complicated than what it actually is.
Thank you,
Zachary Cmiel
The PoKoBros