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.
- To support his daily work
- To get the resources before and after his death.
What are categories of inheritance?
There are two categories of inheritance;- Single inheritance
- Multiple inheritances
Write the syntax to inherit the class from parent class?
Serial# | Code |
1 | class child_class_name : Access_specifier Parent_class_name |
2 | { |
3 | Body of the class; |
4 | }; |
- 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 |