Last modified on February 12th, 2021
Inheritance of classes, Why we need to inherit the classes
What is the inheritance of class?
We can create child classes for a class. Child classes are called derived classes and parent classes are called base classes.
Why do we need to inherit the classes?
Lets an example of daily life.
Suppose John is a person with following properties;
- John has resource. e.g, home, car, bank balance, and a big property.
Now John need his child for the following two reasons;
- To support his daily work
- To get the resources before and after his death.
So, this will leads the John to think that he should have a baby.
Similarly, in OOP, we need to create a child of the parent classes so that when we declare any variable or function in the parent class, it can be shared easily with his child class without wasting time and computer memory.
What are categories of inheritance?
There are two categories of inheritance;
- Single inheritance
- Multiple inheritances
Both of these are discussed in a separate tutorial.
Write the syntax to inherit the class from parent class?
|1||class child_class_name : Access_specifier Parent_class_name|
|3||Body of the class;|
- the class is a keyword to start class. after class, there is a space
- After space we put child class name, then :
- After: we put access specifier for class(private, protected, public)
- After access specifier, there is one space and then parent class name.
|Test Your Understandings|
1.class parent_class_name : Access_specifier child_class_name is the correct syntax for creating child class? YES / NO
2. There are two categories of inheritance?
Inheritance of classes, Why we need to inherit the classes.