Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods fuelAmount , capacity , applyBrakes will be same for all of the three classes. If we create these classes avoiding inheritance then we have to write all of these functions in each of the three classes as shown in below figure:. You can clearly see that above process results in duplication of same code 3 times.
This increases the chances of error and data redundancy. To avoid this type of situation, inheritance is used. If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can simply avoid the duplication of data and increase re-usability. Look at the below diagram in which the three classes are inherited from vehicle class: Using inheritance, we have to write the functions only one time instead of three times as we have inherited rest of the three classes from base class Vehicle.
However, it does inherit a full parent object, which contains any private members which that class declares. Modes of Inheritance Public mode : If we derive a sub class from a public base class.
Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class. Protected mode : If we derive a sub class from a Protected base class. Then both public member and protected members of the base class will become protected in derived class. Private mode : If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class.
Note : The private members in the base class cannot be directly accessed in the derived class, while protected members can be directly accessed. For example, Classes B, C and D all contain the variables x, y and z in below example. It is just question of access. Single Inheritance : In single inheritance, a class is allowed to inherit from only one class.
Syntax :. Please mail your requirement at [email protected] Duration: 1 week to 2 week. The value of a is : 10 The value of b is : 20 Addition of a and b is : Class A? Class B? Enter the value of 'a' : 10 Enter the value of 'b' : 20 Enter the value of c is : 30 Multiplication of a,b,c is : Enter the length and breadth of a rectangle: 23 20 Area of the rectangle is : Enter the base and height of the triangle: 2 5 Area of the triangle is : 5. Reinforcement Learning.
R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning.
Data Structures. Operating System. Computer Network. Compiler Design. The type of inheritance is specified by the access-specifier as explained above. We hardly use protected or private inheritance, but public inheritance is commonly used. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class. Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above.
Previous Page. Next Page. Live Demo.
0コメント