Inheritance:-
Different kinds of objects often have a certain amount in common with each other.
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.
What is need OR advantages using Inheritance:-
1.You can save code memory, easy to make changes. Suppose theres is function which is common between different classes .
We can move it in base class instead of defining same code in multiple classes.
eg.:- For Ui component setting bounds will be used by each UI component. Better to move it in common base class.
Common class is called as base/ Parent and the one which uses it is called as derived/subclass/child class
2.It is also useful when you want to store different kind of classes in same data object. You can make some common class to all that classes . Store the object of that common classes name.
eg. car, truck etc will be stored as vehicle.
Syntax:-
@interface Vehicle :NSObject
@interface Car : Vehicle
Note:- NSObject is supermost class for all the classes having lots of beneficial function to it .
like :- alloc, init etc.
In Objective C multiple inheritance is not allowed.
Multiple inheritance is achieved thru protocol and category.
Derived class get all function and data member from its base class except private.
Overriding is used for to change the base implementation of base class. You can write the same function with signature in derived class. So when u will call that function derived class's function will get called.
How the call to function gets resolved:-
Method Dispatcher:- It does the method resolving work. It decide on basis of object which method to call.
i. If object is of base class then it call base class method.
ii. If object is of derived call then check for method in derived class if not present then goes for base class.
Like C++ in Objective C every method one hidden parameter get passed which is self.
Self is pointer to object on which method get called.
super is used when u want to call the superclass's method from base class.
Different kinds of objects often have a certain amount in common with each other.
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.
What is need OR advantages using Inheritance:-
1.You can save code memory, easy to make changes. Suppose theres is function which is common between different classes .
We can move it in base class instead of defining same code in multiple classes.
eg.:- For Ui component setting bounds will be used by each UI component. Better to move it in common base class.
Common class is called as base/ Parent and the one which uses it is called as derived/subclass/child class
2.It is also useful when you want to store different kind of classes in same data object. You can make some common class to all that classes . Store the object of that common classes name.
eg. car, truck etc will be stored as vehicle.
Syntax:-
@interface Vehicle :NSObject
@interface Car : Vehicle
Note:- NSObject is supermost class for all the classes having lots of beneficial function to it .
like :- alloc, init etc.
In Objective C multiple inheritance is not allowed.
Multiple inheritance is achieved thru protocol and category.
Derived class get all function and data member from its base class except private.
Overriding is used for to change the base implementation of base class. You can write the same function with signature in derived class. So when u will call that function derived class's function will get called.
How the call to function gets resolved:-
Method Dispatcher:- It does the method resolving work. It decide on basis of object which method to call.
i. If object is of base class then it call base class method.
ii. If object is of derived call then check for method in derived class if not present then goes for base class.
Like C++ in Objective C every method one hidden parameter get passed which is self.
Self is pointer to object on which method get called.
super is used when u want to call the superclass's method from base class.
0 comments:
Post a Comment