Comparison between Inheritance and Polymorphism

Inheritance

Inheritance is one of the crucial features of OOP, which strongly support “reusability”. Reusability could be described as creating a new class by reusing the properties of the existing class. In inheritance, there is a base class, which is inherited by the derived class. When a class inherits any other class, the member(s) of the base class becomes the member(s) of a derived class.

Inheritance in Object Oriented Programming

OOPs is a style of computer programming which represents concepts as objects that have state and behavior. Inheritance is one of the most important features of Object-Oriented Programming Languages whether it is Java, C++, or any other OOP language. But what is Inheritance and its need?

Inheritance can be defined as a mechanism by which one object can acquire all the properties (i.e., data members) and behaviour (i.e., member functions or methods) of a parent object to reduce code redundancy. But how does it reduce code redundancy let us see with an example. Suppose you are creating a project where you have many classes now there are some methods which are repeated in many classes so would it be feasible to rewrite the same method again and again in each class? Off course not. So, this is where inheritance comes into picture, we create a class which has the method which is repeated and rather than rewriting the same method again and again we can simply extend that class i.e., inherit its property to other classes. Now as you can see this creates an IS-A relationship, so inheritance is sometimes referred as

is-a relation between class where the class which inherits some methods or behavior from another class in called a child class or subclass and the class from which it extends some properties is called parent class or superclass


Real-Life example of inheritance in java:


Inheritance is the capability of one class to inherit capabilities or properties from another class in Java. For instance, we are humans.

We inherit certain properties from the class ‘Human’ such as the ability to speak, breathe, eat, drink, etc. We can also take the example of cars. The class ‘Car’ inherits its properties from the class ‘Automobiles’ which inherits some of its properties from another class ‘Vehicles’.

Advantages and Disadvantages of inheritance:

Advantages:

  1. Code reusability
  2. By inheritance we can achieve dynamic polymorphism i.e method overriding

Disadvantages:

  1. Using inheritance, the parent and child class gets tightly coupled

 

Polymorphism

Polymorphism is the ability of an object to take on many forms. The word polymorphism is derived from 2 words: Poly and morphs. The word “poly” means many and “morphs” mean forms. So, Word polymorphism means many forms. In chemistry, the term polymorphism means that an object can exist in different crystalline forms. For example, Carbon can exist in three types. Coal, graphite, and diamond are the three different crystalline forms of carbon.

Polymorphism is one of the most important principals of OOPs. In Java, polymorphism is a phenomenon of an object that can exhibit a property of performing operations from different perspectives.

Real Life example of polymorphism in java:

You can imagine an image describing the different roles of a teacher. The teacher is only one, but he/she takes multiple roles like- he/she is a guide, a lecturer, an assistant, a friend, a son/daughter, a father/mother, an interviewer and many more. This is known as polymorphism. When different people enter an organization, a security guard outside the organization acts differently. When the boss arrives, he acts one way, and when the employees arrive, he acts another way. When the customers arrive, the guard will act in a unique way. As a result, the guard's demeanour varies depending on the individual who is arriving.

Use of Java Polymorphism:

Polymorphism allows you to create methods that correctly handle different types of features with the same name. Polymorphism also allows for code consistency.

Advantages and Disadvantages of Polymorphism:

Advantages:

  1. Polymorphism allows a superclass to define methods common to all derived classes, whereas a subclass can define additional implementations of some or all these methods.
  2. Method overriding is supported by dynamic binding or dynamic polymorphism, a key aspect of runtime polymorphism.
  3. Polymorphism allows a method to do different things depending on the object it operates on.

Disadvantages:

  1. Polymorphism ends up raising performance issues in real-time.
  2. Polymorphism reduces the readability of the code.
  3. Programmers find Polymorphism a little challenging to implement.

Comparison Inheritance and Polymorphism

Inheritance permits, code reusability, and polymorphism are the event of one capacity with various structures. The essential distinction between Inheritance and polymorphism is that Inheritance permits the all-around existing code to be reused again in a program, and polymorphism gives a component to progressively conclude what type of a capacity to be invoked.


Comparison Chart

BASIS FOR COMPARISON

INHERITANCE

POLYMORPHISM

Basic

Inheritance is making a new class using the behavior of the previously existing class. 

Polymorphism is basically a common interface for multiple forms.

Implementation

Inheritance is typically implemented in classes. 

Polymorphism is basically implemented on function/methods.

Use

To supports the concept of reusability in Object Oriented Programming and reduces the size of the program. 

Permits object to decide which form of the method to be invoked when at compile time(overloading) as well as run time(overriding).

Forms

Inheritance might be a single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, and hybrid inheritance.

  Polymorphism might be a compile-time polymorphism that includes method overloading or run-time polymorphism which includes method overriding.

example

The class 'table' can inherit the feature of the class 'furniture', as a 'table' is a 'furniture'.

The class 'study_table' can also have function 'set_color()' and a class 'Dining_table' can also have function 'set_color()' so, which form of the set_color() function to invoke can be decided at both, compile time and run time.


Key Differences Between Inheritance and Polymorphism

1. Inheritance is making a class that gets its component from an all-around existing class. Then again, polymorphism is a connection point that can be characterized in numerous structures.

2. Inheritance is executed on the classes though, the polymorphism is carried out on techniques/capacities.

3. As Inheritance permits a determined class to utilize the components and strategies characterized in the base class, the inferred class doesn't have to characterize those components or technique it again thus, we can say it builds code reusability and henceforth, decreases the length of the code. Then again, polymorphism makes it workable for an item to conclude what type of strategy it needs to invoke at both compile-time and run time.

4. The inheritance can be classified as single inheritance, multiple inheritances, multilevel inheritance, hierarchical inheritance, and hybrid inheritance Then again, polymorphism is classified as overloading and overriding.


Conclusion

Inheritance and polymorphism are interrelated ideas, as the dynamic polymorphism applies to the classes which additionally carry out the idea of inheritance.




Comments

  1. nice ,very much informativeπŸ‘

    ReplyDelete
  2. Excellent work and very helpful information guys

    ReplyDelete
  3. very nice… I really like your blog…

    ReplyDelete
  4. Highly Informative πŸ‘πŸΎGood job

    ReplyDelete
  5. Great informative skills. πŸ’―πŸ‘πŸ»

    ReplyDelete
  6. Very impressive ,with proper representation πŸ‘

    ReplyDelete
  7. Really nice work πŸ‘πŸΌ

    ReplyDelete
  8. Really informative. Learnt a lot .

    ReplyDelete
  9. Very informative learned a lot . This will probably help me in becoming a good java programmer.

    ReplyDelete

Post a Comment