Overview
Inheritance is a relationship between classes wherein one class is derived from another class. The derived class is called child class, and the class from which it is derived is called parent class. Inheritance is also called as parent–child relationship between classes such that child class gives a specialized implementation of parent class. Reusability of the code is the key principle of inheritance because the properties defined in the parent class are borrowed by the child class. Inheritance is one of the most important features of object oriented programming, wherein the classes are organized in a hierarchical form. The class at the top of hierarchy is called parent class, whereas the class at the bottom of the hierarchy is called child class. Advantage of inheritance is that the members (variables and functions) defined by the parent class are also made available into the child class. This saves the programmers efforts to redevelop logic in child class, which is already implemented in parent class. Along with the properties defined in parent class, the child class can also have additional properties of its own as shown in the Figure 14.1. Hence the parent class is also called Base class/Super class/Generalized class and the child class is also called Derived class/Sub class/Specialized class.
As seen from the Figure 14.1, the member1 and member2 are defined in the parent class, which are available by default, in the child class. Also, child class can define additional members such as member3 and member4. This means that if you create an object of parent class, it will contain only two members: member1 and member2; whereas, if you create an object of child class, it will contain four members: member1, member2, member3, and member4. Therefore, a parent class should define the common properties that are required by all the child classes, whereas each of child class should define the specialized properties applicable only for that child.
As an example, let's assume that we need to store data about students and professors in a particular institute in computer memory. The requirement is that for every person(a student or a professor)we need to store name, date of birth, and gender.
Review the options below to login to check your access.
Log in with your Cambridge Aspire website account to check access.
If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.