1.

What Is Difference Between Polymorphism And Inheritance?

Answer»
  • Inheritance defines parent-child RELATIONSHIP between two classes, polymorphism take advantage of that relationship to add dynamic behaviour in your code.
  • Inheritance helps in code reusability by allowing child class to INHERIT behavior from the parent class. On the other hand Polymorphism allows Child to redefine already defined behaviour inside parent class. Without Polymorphism it's not possible for a Child to execute its own behaviour while REPRESENTED by a Parent reference variable, but with Polymorphism he can do that.
  • Java doesn't allow multiple inheritance of classes, but allows multiple inheritance of Interface, which is actually require to implement Polymorphism. For example a Class can be Runnable, Comparator and SERIALIZABLE at same time, because all three are interfaces. This makes them to pass around in code e.g. you can pass instance of this class to a METHOD which accepts Serializable, or to Collections.sort() which accepts a Comparator.
  • Both Polymorphism and Inheritance allow Object oriented programs to evolve. For example, by using Inheritance you can define new user types in an Authentication System and by using Polymorphism you can take advantage of already written authentication code. Since, Inheritance guarantees minimum base class behaviour, a method depending upon super class or super interface can still accept object of base class and can authenticate it.



Discussion

No Comment Found