This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Concept of inheritance vs polymorphism  RSS feed

 
Justin Tan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
   I have some confusions regarding the concept of inheritance and polymorphism.

Concept of Polymorphism:
-one object having multiple forms.
-achieved via method overloading and method overriding.

Concept of inheritance:
- The sub class inherits variables and methods from the parent class.
- Method overriding is done by sub class to be precise.




In this code, the object of the parent class is formed in two different forms, one form(employee) , other form(student), has polymorphism been achieved since the definition says an object having multiple forms and method over riding is also performed in it. Does that mean the different form of an object is based on the methods? Does that mean it is always the object of the parent class that undergoes polymorphism or is it possible for an object from sub class to achieve polymorphism?.
If I had done Employee p = new Employee(); and Student a = new Student(); to create the objects would it have been wrong in the case of polymorphism?.
To me, it seems that inheritance and polymorphism are the same concept since both relies on the parent class to provide variables and methods to the sub class. The concept of inheritance is to promote re usability of code
since we would not need to write the same code for all the classes. So why is polymorphism necessary since it is the same as inheritance?.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Practically everything you wrote is incorrect. Rather than going through and correcting each point, it might be better if you first reviewed the following articles to get a better understanding of the concept:

https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Tan wrote:If I had done Employee p = new Employee(); and Student a = new Student(); to create the objects would it have been wrong in the case of polymorphism?.
To me, it seems that inheritance and polymorphism are the same concept since both relies on the parent class to provide variables and methods to the sub class. The concept of inheritance is to promote re usability of code
since we would not need to write the same code for all the classes. So why is polymorphism necessary since it is the same as inheritance?.


hello tan,welcome to ranch.
actually you are trying to mess the two different concept of object oriented programming.
1.Inheritance
it is actually deriving a class from existing one as in your case it is person class and doing so will gain you all the properties
of the parent class(Accordingly with the access modifiers).it will all depend on a programmer whether or not he wants to add
some extra methods(or overriding the existing if possible) or any other identifier.

2.Polymorphism
consider this part of your code:-

how would the jvm know at runtime that it needs to call the walk method of student class,here it is this property-polymorphism
which drives the jvm in calling the method of the desired class.

so concisely it is inheritance that entails to gain the parent class property.and polymorphism in action during when you are
implementing it(which class to choose).

i would also recommend you to better go through the oracle tutorial as junilu mentioned.they really make you learn better that's
what they are made for.
hope it helps.

regards
praveen
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Their is a typo in the code section replace that by the following one:-
 
Justin Tan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
   Thanks and I have checked the 2 links above. I just want to check if I am moving to the right direction about my understanding concept of inheritance and polymorphism.

1 ) Inheritance





2) Polymorphism

 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems you've made a little progress with inheritance but I don't think you get the point of polymorphism yet.  You can help yourself tremendously by writing and experimenting with code that actually compiles and runs. None of the code that you posted will actually compile. Java is case-sensitive and the keyword is public, not Public

You should also try to follow standard Java coding conventions, particularly when it comes to capitalization of names. Following conventions makes your programs easier for others to read.

Now, as far as polymorphism is concerned, it is really about the ability to use a reference to a class in your program and not care whether or not the object that's actually referenced is an instance of that class or an instance of one its subclasses. Inheritance can also play a role here but polymorphism can also be achieved through interfaces, as explained in that second article I referenced before.

However, the code that you gave doesn't really show the power of polymorphic behavior.

Consider this class hierarchy:

In this hierarchy, Payment is a superclass and CashPayment and CheckPayment are its subclasses. Payment defines a showDetails() method and its subclasses inherit and override it. Each has it's own implementation of showDetails() that behaves differently from the others.

The following is how you would see polymorphic behavior in action:

Lines 3-7 create an array that contains three new objects: a Payment, a CashPayment, and a CheckPayment object. Line 9 sets up a loop that iterates over the payments array. The call on line 10 is polymorphic because the variable p is declare to be a Payment but the actual object that it will reference will change from a Payment, to a CashPayment, to a CheckPayment as the for-loop iterates over the array.  In this example, the three different types of objects that the reference variable p is able to work with are the different "forms" referred to by the definition of polymorphism. That is, even though the code declares the loop variable p as a Payment object, it will still work with a CheckPayment object or a CashPayment object because these are just two special "forms" of Payment.

If you decide to add new subclasses of Payment later on, you could expand the payments array to include new kinds of Payment objects and the code on lines 9 and 10 will still work without any modification.

In summary, polymorphism is about writing code that uses a reference of type S that will still work with objects that are subclasses of S, even those that will only be created some time in the future! When designed and used properly, taking advantage of polymorphism can lead to code that is simpler, more robust, and more flexible.
 
Justin Tan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu Lacar Thank you for your reply. I think i understood the concept better after your post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!