Java : difference between Polymorphism and Inheritance
August 25, 2015 Leave a comment
Since the definition for both them have some common ground so it may confuse some times with the differences between this two paradigm. Definitions from wiki, you might have already read it.
- Polymorphism : polymorphism is the provision of a single interface to entities of different types. Java supports basically subtype(using inheritance in Java) and parametric polymorphism (using generics in Java).
- Inheritance (OOP) is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior).
So from the above definition,
- Inheritance or subclassing in Java is one of the technique used to achieve polymorphism.
- Inheritance refers to using the structure and behavior of a superclass in a subclass.
- Inheritance helps code reuse at the same time while achieving polymorphism.