Polymorphism and Inheritance in Goal C

I have an encoding in Objective-C and you want to create a base controller and use polymorphism to create subclasses. I also follow the Stanford CS193p course. Since polymorphism uses common methods for different classes, how does xcode know which function from each subclass to call if they have the same function name?

In addition, a function is called in the base class, but returns nil and has a comment with an abstract. This function is implemented in the subclass controller and returns a value. Why does the base controller call a subclass function if it is lower in the hierarchy? Why doesn't it return zero?

+4
source share
3 answers

polymorphism:

Polymorphism in Objective-C is a fairly standard subtype polytype, which you will find in most class-oriented object-oriented languages. This means that an instance of a class can also be considered an instance of any of its superclasses.

In more specific terms, this means that your custom subclass of UITableViewCell, which we will call MyCustomTableViewCell, can be used wherever a UITableViewCell is expected. If the method has a UITableViewCell parameter, you can pass an instance of your MyCustomTableViewCell.

The converse is not true; if the 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 uses all UITableViewCell methods (e.g. prepareForReuse). Method implementations can be overridden, but you can still send them the same messages.

Keep in mind that accessing an instance of MyCustomTableViewCell as a UITableViewCell does not change its behavior. If you override prepareForReuse, you still get your overridden behavior.

Inheritance:

The concept of inheritance brings something real to programming. It allows you to define a class that has a specific set of characteristics (such as methods and instance variables), and then other classes that must be created that are derived from this class. A derived class inherits all the functions of the parent class and usually adds some of its functions.

Deriving classes, we create what is often called a class hierarchy. The class at the top of the hierarchy is known as the base class or root class, and the derived classes are known as subclasses as well as child classes. Any number of subclasses can be obtained from the class. The class from which the subclass is derived is called the parent class.

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

Tutorials:

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

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

Literature:

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

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

+17
source

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 the behavior you need that is not in the superclass. Usually you also call a function in a superclass. When you call a function from another object, the function from the deepest superclass is called. This is called polymorphism. If this did not work, you can never add functionality to classes.

Below, some could help me 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 
0
source

Xcode does not know whether an object (actually a pointer) of type UIViewController points to an object of type a subclass, such as MyViewController . This will be clarified at runtime.

At some point during your program, you will have to β€œ alloc init ” your MyViewController and that when a bit of memory is reserved for an object of type MyViewController . Subsequently, you can let an object of type UIViewController point to this bit of memory, but the memory itself will not be changed. If a function is defined both in the base class of the UIViewController and in the subclass of MyViewController , it is called overridden, and this information is also contained in this memory bit.

Using this fragment, you can check objects and their memory addresses of functions:

 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 subclass, the function of the base class will be called (one class is higher in the inheritance chain).

Also, in objective-c, you say send message to object, not class function to emphasize this fact.

Check out Apple's docs on messaging and inheritance details!

0
source

Source: https://habr.com/ru/post/1487423/


All Articles