Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism and Inheritance in Objective C

I have a coding in Objective C and want to make a base Controller and use polymorphism to create subclasses. I am also following CS193p stanford course. Since polymorphism uses general methods for different classes how does xcode know which function from each subclass to call if they have the same function name?

Also, in the base class a function is called but returns nil and has a comment saying abstract. This function is implemented in the subclass controller and returns a value. How come the base controller will call the subclass function if it is lower in the hierarchy? Why doesn't it return nil?

like image 951
user2076774 Avatar asked Dec 01 '22 19:12

user2076774


1 Answers

Polymorphism:

Polymorphism in Objective-C is the fairly standard subtype polymorphism you find in most class based object oriented languages. It means that an instance of a class can also be treated as an instance of any of its superclasses.

In more concrete terms, it means that your custom UITableViewCell subclass, which we'll call MyCustomTableViewCell, can be used anywhere a UITableViewCell is expected. If a method has a UITableViewCell parameter, you can pass an instance of your MyCustomTableViewCell.

The converse is not true; if a method expects an instance of MyCustomTableViewCell, passing a regular UITableViewCell is an error.

This works because subclasses inherit the interface of their superclass. MyCustomTableViewCell automatically has all the methods of UITableViewCell (like prepareForReuse). The method implementations may be overriden, but you can still send it the same messages.

Keep in mind that treating an instance of MyCustomTableViewCell as a UITableViewCell doesn't change its behaviour. If you've overriden prepareForReuse, you'll still get your overridden behaviour.

Inheritance:

The concept of inheritance brings something of a real-world view to programming. It allows a class to be defined that has a certain set of characteristics (such as methods and instance variables) and then other classes to be created which are derived from that class. The derived class inherits all of the features of the parent class and typically then adds some features of its own.

By deriving classes we create what is often referred to as a class hierarchy. The class at the top of the hierarchy is known as the base class or root class and the derived classes as subclasses or child classes. Any number of subclasses may be derived from a class. The class from which a subclass is derived is called the parent class.

Classes need not only be derived from a root class. For example, a subclass can also inherit from another subclass with the potential to create large and complex class hierarchies. In Objective-C a subclass can only be derived from a single direct parent class. This is a concept referred to as single inheritance.

Tutorials:

http://www.tutorialspoint.com/objective_c/objective_c_inheritance.htm

http://www.tutorialspoint.com/objective_c/objective_c_polymorphism.htm

References:

http://www.quora.com/In-Objective-C-what-is-polymorphism

http://www.techotopia.com/index.php/Objective-C_Inheritance

like image 134
Imran Avatar answered Dec 10 '22 13:12

Imran