1

Can inheritance exist without Polymorphism or is it an imminent side effect of it? It might be a fallacious question, but it will help me understand the relation between both.

mowienay
  • 1,264
  • 4
  • 19
  • 32

2 Answers2

2

Can inheritance exist without Polymorphism?

They are related concepts, but yes, it is possible to have one without the other. In Java if you subclass a parent class you also get a subtype, but in other languages this might not be the case implicitly. In some languages inheritance can be just a form of code reuse.

For example, in C++ you don't get polymorphism if you don't mark your methods virtual. See here for an explanation: Why do we need Virtual Functions in C++?. In Java on the other hand, all public methods are implicitly virtual.

This is a vast subject that has many flavors in many languages. As a TL;DR (and a gross gross gross simplification) you can think inheritance is a form of code reuse while polymorphism is the ability to substitute an object of a type with an object of a subtype and your program continues to work correctly. In Java these two things overlap and you get one from the other but not all languages are like that.

And even if you get polymorphism out of the box from inheritance it is still possible to "break polymorphism" by not respecting the Liskov substitution principle. Like I said... a vast subject.

Community
  • 1
  • 1
Bogdan
  • 23,890
  • 3
  • 69
  • 61
0

If you look closely they are actually related to each other, because its Inheritance which makes Polymorphism possible, without any relationship between two class, it's not possible to write polymorphic code, which can take advantage of runtime binding of different objects.

You cannot use Polymorphism on something which is not inherited by Child class e.g. private method can't be overridden in Java.

Like in real world, Inheritance is used to define the relationship between two classes. It's similar to Father-Son relationship. In object oriented programming, we have a Parent class (also known as the super class) and a Child class (also known as the subclass). Similar to the real world, Child inherits Parents qualities, e.g. it's attribute, methods and code. Inheritance is actually meant for code-reuse. A child can reuse all the codes written in Parent class, and only write code for behavior which is different than the parent. Though it’s possible to restrict something to parent itself by using the private and final keyword in Java.On the other hand, Polymorphism is an ability of Object to behave in multiple form.

Read more: http://java67.blogspot.com/2014/04/difference-between-polymorphism-and-Inheritance-java-oops.html#ixzz4A201Ln9T

And from the following topic

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.

Community
  • 1
  • 1
Sir1
  • 732
  • 1
  • 8
  • 18
  • I think polymorphism can exist without inheritance. For example, you can achieve it by interfaces where objects implementing the same interface can stand for each other in specific cases – mowienay May 29 '16 at 09:31
  • yes, actually it is a personal opinion, I think they are both from a more general concept name inheritance, Inheritance describes 'is a' relationships, interfaces describe 'behaves like' relationships. in both case some thing will be inherited, but in the case you are right :) – Sir1 May 29 '16 at 09:58