What is the difference between Inheritance and Polymorphism?

What is the difference between Inheritance and Polymorphism?

The main difference is polymorphism is a specific result of inheritance. Using Inheritance is only one of the ways in which Polymorphism is achieved in Java. See Polymorphism

Inheritance Polymorphism
refers to using the structure and behavior of a super class in a subclass. refers to changing the behavior of a super class in the subclass.
A (derived) class inherits fields and methods from all its superclasses, whether direct or indirect. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
Inheritance always between 2 OR more classes / interfaces. Polymorhism can be possible in a class / interface
Inheritance always conform “is-a” relationship Polymorhism doesn’t always confirm “is-a” relationship. Polymorhism supports both “is-a” and “has-a” relationships
Inheritance also describes data members, etc. Polymorphism deals with method invocation
Inheritance is more a static thing (one class extends another) Polymorphism is a dynamic/ runtime thing (an object behaves according to its dynamic/ runtime type not to its static/ declaration type). Though the static/ declaration type of a is A, the actual dynamic/ runtime type is B and thus a.foo() will execute foo as defined in B not in A.
It is basically applied to classes. Whereas it is basically applied to functions or methods.
Inheritance supports the concept of reusability and reduces code length in object-oriented programming. Polymorphism allows the object to decide which form of the function to implement at compile-time (overloading) as well as run-time (overriding).
Inheritance can be single, hybrid, multiple, hierarchical and multilevel inheritance. Whereas it can be compiled-time polymorphism (overload) as well as run-time polymorphism (overriding).
It is used in pattern designing. It is used in pattern designing.

https://stackoverflow.com/questions/6308178/what-is-the-main-difference-between-inheritance-and-polymorphism

Inheritance is when a ‘class’ derives from an existing ‘class’. So if you have a Person class, then you have a Student class that extends Person, Student inherits all the things that Person has. There are some details around the access modifiers you put on the fields/methods in Person, but that’s the basic idea. For example, if you have a private field on Person, Student won’t see it because its private, and private fields are not visible to subclasses.

Polymorphism deals with how the program decides which methods it should use, depending on what type of thing it has. If you have a Person, which has a read method, and you have a Student which extends Person, which has its own implementation of read, which method gets called is determined for you by the runtime, depending if you have a Person or a Student. It gets a bit tricky, but if you do something like

Person p = new Student();
p.read();

the read method on Student gets called. Thats the polymorphism in action. You can do that assignment because a Student is a Person, but the runtime is smart enough to know that the actual type of p is Student.


In Java, the two are closely related. Because Java uses Dynamic Method Dispatch or Late Binding or Runtime Polymorphism in Java


Links to this note