user2076774
user2076774

Reputation: 405

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?

Upvotes: 4

Views: 11276

Answers (3)

Imran
Imran

Reputation: 1715

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

Upvotes: 18

user511
user511

Reputation: 154

Xcode doesn't know if an object (actually a pointer) of type UIViewController is actually pointing to an object of a subclass type, say MyViewController. This will be figured out at runtime.

At some point during your program you'll have to "alloc init" your MyViewController and that's when a bit of memory is reserved for an object of type MyViewController. Afterwards you could let an object of type UIViewController point to that bit of memory but the memory itself won't be changed. If a function is defined both in the base class UIViewController and in the sub class MyViewController it's said to be overridden and this information is also contained in that bit of memory.

With this snippet you can examine the objects and their function's memory addresses:

MapViewController *m = [[MapViewController alloc] init];
NSLog(@"MapViewController at memory location %p with function loadView %p", m, [m methodForSelector:@selector(loadView)]);
UIViewController *v = m;
NSLog(@"UIViewController at memory location %p with function loadView %p", v, [v methodForSelector:@selector(loadView)]);

UIViewController *v2 = [[UIViewController alloc] init];
NSLog(@"UIViewController at memory location %p with function loadView %p", v2, [v2 methodForSelector:@selector(loadView)]);

If the function is not defined in the sub class the function of the base class (one class higher in the inheritance chain) will be called.

Moreover, in objective-c you say "send a message to an object" rather than the "function of a class is called" to stress this fact.

Check out the Apple docs on the gory details of messaging and inheritance!

Upvotes: 0

Raymond
Raymond

Reputation: 3670

When a class inherits from another class and implements functions that are also implemented in the superclass it overrides the implementation in the superclass. You do this to add behaviour that you need that is not present in the superclass. Usually you will also call the function in the superclass. When you call the function from another object the function from the deepest superclass will be called. That is called polymorphism. If it did not work this way you would never be able to add functionality to classes.

Below some could that helped me to understand the principle.

//  In pseudo code

class Figure 
  def area
    return length * width
  end

end

class Rectangle < Figure
// Don't need to add a different area calculator, the default is good
end


class Circle < Figure
  def area
     return PI * radius *radius 
  end
end


figures = [Circle.new .., Rectangle.new .. ]

// Now loop over all the figures, we are not interested in what figures exactly are
figures.each do |figure|
  total_area =  total_area + figure.area
end

Upvotes: 0

Related Questions