Inheritance - Objective C
One of the most important concept in object - oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
When creating a class,instead of writing completely new data members and member function, the programmer can designate that the new class should inherit the member of an existing class. This existing class is called the base class, and the new class is referred as the derived class.
The idea of inheritance implements the is relationship For example, mammal IS-A animal, dog IS-A mammal, hence dog IS-A animal as well and so on.
Base & derived classes:
Objective C allows only multiple inheritance i.e. it can have only base class but allows multilevel inheritance. All classes in Objective C is derived from the supercalss NSObject.
Consider a base class Shape and its derived class Rectangle follows:
Access Control and Inheritance
A derived class can access all the private members of its base class if it;s defined in the interface class, but it cannot access private members that are defined in the implementation file.
We can summarize the different access types according to who can access them in the following way
When creating a class,instead of writing completely new data members and member function, the programmer can designate that the new class should inherit the member of an existing class. This existing class is called the base class, and the new class is referred as the derived class.
The idea of inheritance implements the is relationship For example, mammal IS-A animal, dog IS-A mammal, hence dog IS-A animal as well and so on.
Base & derived classes:
Objective C allows only multiple inheritance i.e. it can have only base class but allows multilevel inheritance. All classes in Objective C is derived from the supercalss NSObject.
@interface derived-class : base-class
Consider a base class Shape and its derived class Rectangle follows:
Access Control and Inheritance
A derived class can access all the private members of its base class if it;s defined in the interface class, but it cannot access private members that are defined in the implementation file.
We can summarize the different access types according to who can access them in the following way
- Variables declared in implementation file with the help of extension is not accessible.
- Methods declared in implementation file with the help of extension is not accessible.
- In case the inherited class implements the method in base class , then the method in derived class is executed.
No comments: