Science, asked by aayyuusshhii496, 1 year ago

Inheritance – advantages of inheritance in oop

Answers

Answered by MKbhatia
0
Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class.Reusability enhanced reliability. The base class code will be already tested and debugged.As the existing code is reused, it leads to less development and maintenance costs.Inheritance makes the sub classes follow a standard interface.Inheritance helps to reduce code redundancy and supports code extensibility.Inheritance facilitates creation of class libraries.

 

Disadvantages of inheritance are as follows:

Inherited functions work slower than normal function as there is indirection.Improper use of inheritance may lead to wrong solutions.Often, data members in the base class are left unused which may lead to memory wastage.Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.

When a base class and sub class contains a function with the same signature, and the function is called with base class object, then the function in derived class executes and the function in base class is said to be overridden. This is known as function overriding. Following program demonstrates function overriding.


#include using namespace std; class A { protected: int x; public: void show() { cout<<"x = "<<x<<endl; } }; class B : public A { protected: int y; public: B(int x, int y) { this->x = x; this->y = y; } void show() { cout<<"x = "<<x<<endl; cout<<"y = "<<y<<endl; } }; int main() { A objA; B objB(30, 20); objB.show(); return 0; } Output for the above program is: x = 30 y = 20

In the above program, both super class A and sub class B contains the same function show() with same signature (function name plus parameters). So, when sub class object is used to call show(),function in sub class B executes overriding the function in super class A.

We know that when a base class pointer refers to a derived class object, the extra features in derived class are not available. To access the extra features in the derived class, we make the functions in the base class as virtual.

Take your time to comment on this article.


Answered by Anonymous
1

Hey !

Advantages of Inheritance in OOPs :

-The main advantage of the inheritance is that it helps in reusability of the code. The codes are defined only once and can be used multiple times. In java we define the super class or base class in which we define the functionalities and any number of child classes can use the functionalities at any time.

-Through inheritance a lot of time and efforts are being saved.

-It improves the program structure which can be readable.

-The program structure is short and concise which is more reliable.

-The codes are easy to debug. Inheritance allows the program to capture the bugs easily

-Inheritance makes the application code more flexible to change.

-Inheritance results in better organisation of codes into smaller, simpler and simpler compilation units.

Similar questions