• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism with the same method  RSS feed

 
Kunder Akshay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the following simple code snippet demonstrating Method Overriding.



My doubt goes as following :

In the main method when we have Two t = new Two()  or One t = new Two() , we get the same output when we call the meth() method.
What is the difference between the two actually.
As in the Second statement One t = new Two() I am taking Parent class reference to Child class Object. Is this Dynamic Method Dispatch?
 
Les Morgan
Rancher
Posts: 756
18
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you expect?  You have 2 object containing methods with the same signatures.  Since Two extends One, of course you can assign a Two to a One reference variable, but why would you expect anything different than to have the same answers when you are indeed calling the very same method--it does have the same signature.  The polymorphism is that you can assign the Two to a reference variable expecting a One because in fact it is a One also.

When you instantiate the object, you get the Two constructor, not the One constructor, because you are creating two Two objects.
 
Kunder Akshay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:What did you expect?  You have 2 object containing methods with the same signatures.  Since Two extends One, of course you can assign a Two to a One reference variable, but why would you expect anything different than to have the same answers when you are indeed calling the very same method--it does have the same signature.  The polymorphism is that you can assign the Two to a reference variable expecting a One because in fact it is a One also.

When you instantiate the object, you get the Two constructor, not the One constructor, because you are creating two Two objects.



Thank you so much mate.
 
Sami Kassoum
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aksh Kdr wrote:Please go through the following simple code snippet demonstrating Method Overriding.

In the main method when we have Two t = new Two()  or One t = new Two() , we get the same output when we call the meth() met


How do you get the same output when you override the meth() method in class two and you call it with different String argument ?!
 
Campbell Ritchie
Sheriff
Posts: 54034
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac MethOverRide.java
java MethOverRide
Base Class Constructor.
String in Base Class : Dumbbbbbb
------------------
Base Class Constructor.
Derived Class Constructor.
String in Derived class : Derived dumbbbb
You don't get the same output. You cannot even cast and get the same output. Change line 48 to ((One)t)s.meth("Derived dumbbbb"); and there is no change in the output because polymorphism still applies.
Like many people here, you have not very good names for the classes: One and Two don't help understand the code. Had you called them Superclass and Subclass they would have been easier to understand. The terms derived and base are not usually used in Java®; we say superclass and subclass. They say derived and base in C#, but derived and base are good terms which describe the different classes quite well.

And ... welcome to the Ranch
 
Sami Kassoum
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks campbell for the information about casting the object it was new to me.
here I wrote below my "improved" version of the original code, I think the title was about Polymorphism but actually the code given was about method overloading.
so I wrote a small class called Polymorphism that have a method that demonstrate what polymorphism actually is.



 
Dave Tolls
Ranch Hand
Posts: 2554
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do get the same output.
The OP is talking about changing this:

to this:
 
Campbell Ritchie
Sheriff
Posts: 54034
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you make that change you will get the same outputs. With or without casts. Sorry, I missed that bit.
 
Sami Kassoum
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:You do get the same output.
The OP is talking about changing this:

to this:


What if I typecast the t  back to the Two like this:
One t = new Two();
((Two)t).meth("Derived dumbbbb");  will we get  the same output ?(sorry ,I am writing from my phone)
 
Campbell Ritchie
Sheriff
Posts: 54034
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sami Kassoum wrote:. . . if I typecast the t  back to the Two like this:
One t = new Two();
((Two)t).meth("Derived dumbbbb");  will we get  the same output ?(sorry ,I am writing from my phone)
Look in the Java® Language Specification (=JLS), which will tell you that an identity conversion to the same type is always trivially permissible. The javac tool might give a warning about an unchecked cast (not called typecast), but you can ignore the warning.

Since you are converting something to the same type it was all along, yes you are going to get the same output.
The code, with a ((Two)t). cast wrote:java MethOverRide
Base Class Constructor.
String in Base Class : Dumbbbbbb
------------------
Base Class Constructor.
Derived Class Constructor.
String in Derived class : Derived dumbbbb
Your writing from your phone is much better than some other people's
 
Les Morgan
Rancher
Posts: 756
18
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, miss read the code, I thought it was assigning a Two to each.

Kunder Akshay wrote:
Les Morgan wrote:What did you expect?  You have 2 object containing methods with the same signatures.  Since Two extends One, of course you can assign a Two to a One reference variable, but why would you expect anything different than to have the same answers when you are indeed calling the very same method--it does have the same signature.  The polymorphism is that you can assign the Two to a reference variable expecting a One because in fact it is a One also.

When you instantiate the object, you get the Two constructor, not the One constructor, because you are creating two Two objects.



Thank you so much mate.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!