Inheritance

A key concept in OOP is inheritance. Inheritance allows one class to inherit functionality from another class. The class being inherited from is the parent class, the class that inherits is the child class. Another way to say this is that the child class derives from the parent class.

The child class can access all the variables and functions of its parent class. In addition to access, the child class can provide its own implementation for any function inherited from the parent class. The child class re-implementing a function of a parent class is known as function overriding.

Inheritance does not have to be linear. Single inheritance allows one object to inherit functionality from only one direct ancestor. Multiple inheritance allows one child class to have multiple parent classes.

Multiple inheritance has some inherent problems. One of the biggest problems of multiple inheritance is the diamond problem. The diamond problem involves having two base classes, let's say class A and class B. Both classes implement a function named foo. When the child class tries to access foo, should it access the inherited function from class A or from class B?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.129.194