0

So I am reading head First Java book and when it explains inheritance right after that it goes to polymorphism without saying how are they connected or what is the benefit.

It just says it makes your code flexible and others can reuse your code. To be honest it's so confusing. I know what inheritance is and can't say I understand polymorphism.

I have tried looking at some YouTube videos and what I understand from polymorphism is you have a superclass and subclasses and you use array to loop through the subclasses to do some functions.

Shalu T D
  • 3,921
  • 2
  • 26
  • 37
  • inheritance used for the code re usability while on the other code polymorphism used for the applying the runtime implementation of override method – Umair Mubeen Jul 10 '20 at 14:54
  • Please check the answer https://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading – Dijesh Jul 10 '20 at 15:13
  • Hello Mir and thank you for your question. As a suggestion, try to keep the questions concise and objective. Also, try using the search to find similar questions. As this is a fundamental Java concept other surely had the same question. Example: https://stackoverflow.com/questions/11732422/is-polymorphism-possible-without-inheritance?r=SearchResults&s=4|114.7178 – Grasshopper Jul 10 '20 at 15:14

2 Answers2

0

Inheritance used for code reusability , while in polymorphism you overload a function that using a same name for a method/function but being able to perform different task , compiler know when to use which because of their different signature i.e. maybe their paarameters are different or return type etc.

0

Inheritance is NOT for code reuse, it is for substitution.
When thinking inheritance, code reuse is incidental.

A lot of developers think reusability when thinking inheritance but be aware, it's a wrong reasonning.
You should favor composition for code reusability over inheritance.

When you think substitution, then you can think inheritance.

A practical example to understand polymorphism is when you look at the design pattern named "Strategy".

Strategy allows to change algorithm at runtime without altering the orchestrator component.

For instance, imagine a video game when you manage some soldiers with evolving weapons.

Main algorithm (orchestrator algorithm) is about moving the soldier and hitting any enemy.
Level 1, you have a knife.
Level 2, you will have a gun.
Level 3, you will have a bomb. Etc.

You don't want to explicitly deal with any kind of weapon within your main algorithm, you want a pseudo code like this inside let's say a class Soldier:

hit() {
   weapon.use(); // so generic ! 
} 

instead of :

hit() {
  if(weapon instance of Knife)
     ....
  else if(weapon instance of Gun) 
     ....
  else if(weapon instance of Bomb) 
     ....
  // what if you create another weapon for a further level...you would have to alter this class to add another 'else if' clause, bad!
} 

So to use the first way being the more concise and solid way, you will have to make usage of polymorphism, by subclassing Weapon class with appropriate subclasses (Knife, Gu, Bomb) implementing the use method.

Polymorphism = weapon can take several forms/structures/behaviors at runtime.

So in this case you are using composition to change the used weapon at runtime, and you are using polymorphism to let the orchestrator ignorant of the exact weapon used.

It makes code flexible, robust, with easy maintenance.

Mik378
  • 21,881
  • 15
  • 82
  • 180