• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism : unexpected result ! why  RSS feed

 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
according to cathy sierra and bert bates - author of a famous java book.

"the reference variables type determines the method that can be invoked on the object the variable is referencing."

class Base{

void display(){
System.out.print("Base Class");
}
}
class Sub extends Base{

}
class test{
public static void main(String a[]){
Base b = new Sub();

considering the above example , it means

b.display() would call the inherited method display() on the object sub because type of reference variable b will decide which method will be called. there is an exception to this rule when there is an overriding method in the subclass. to change the above example accordingly

class Base{

void display(){
System.out.print("Base Class");
}
}
class Sub extends Base{

void display(){
System.out.print("Sub Class");
}
}
class test{
public static void main(String a[]){
Base b = new Sub();

now when there is an overriding method a call to b.display() would result in an actual call to overriden method, in other words now the selection of method depends on the object the reference variable is pointing to, rather than the type of the reference variable.

now see how this rule is flouted

class Base{

void display(){
System.out.print("Base Class");
}

}
class Sub1 extends Base{

void display(){
System.out.print("Sub1 Class");
}

}
class Sub2 extends Sub1{

void display(){
System.out.print("Sub2 Class");
}

}

class Sub3 extends Sub2{

}

class test{
public static void main(String a[]){
Base b = new Sub3();
b.display();

Base class has 1 display() method, Sub1 class has two display methods one inherited and the other is overridding, Sub2 has three - two inherited and one overridding. now sub three will have three display methods - all inherited from above classes. because there is no overriding method in the Sub3 class, the type of reference variable must decide which method to be called. but the result turns out very unexpected .

the output is

Sub2 Class

why this anamolous result???
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all inherited from above classes. because there is no overriding method in the Sub3 class, the type of reference variable must decide which method to be called. but the result turns out very unexpected .


I think you are misinterpreting the statement, and using a very convoluted example to do it. When you call a method, it finds the latest overridden version (for that object) and calls it -- that's it.

The "reference variables type determines the method" is used for overloading, and needs to be considered when you are mixing overloading and overriding -- meaning you must use the reference type to determine the correct overloaded method to call, and then use the actual object type to call the lastest overridden version of that method.

Henry
 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks henry,i take your explanation for this, but exploring the answer for this anamoly(as i felt) i got three different interpretation by different professional developers and all the interpretations seem right. i am confused !

first interpret.

"at runtime the subclass will have just one associated method and that will be the overridden method it means at runtime the inherited method will be replaced by overridden so the object will have just one associated method and that will be the overridden method"

second interpret.

"the Virtual Machine always searches for the overridden method first if it doen not find then it runs the inherited base class method"

third interpret (as you say)

"the latest overridden method will be executed"

i have doubts with the first interpretation because the following contradicts.

class Base
{
void display(){System.out.print("base");}
}
class Sub extends Base{
void display()
{
super.display();
System.out.print("base");
}
}

had there been just overriden method associated with the subclass at runtime. it would not have been possible to call the inherited version using super.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think all four explanations you were given were equivalent to each other. It must really make your application easy to understand if you override a method which prints "base" by a method which also prints "base"! In that case it is in fact the last overridden version you are calling, and the super. call calls the version in its superclass which is the next version up the inheritance tree.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your subclass does not have several versions of each method, but only one, the "last overridden" version.
 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the revised class

class Base
{
string s = "I COME FROM BASE CLASS";

void display() //1
{
System.out.println(s);
}

}
class Sub extends Base{

string s = "I COME FROM SUBCLASS";

void display() //2
{
super.display(); //3
System.out.println(s); //4
}
}

class TestPoly{

public static void main(String a[]){

Base b = new Sub();
b.display()


now if the output is :-

I COME FROM SUBCLASS
I COME FROM SUBCLASS

it means the line //3 is executing the inherited version. now because this version is acting on data belonging to subclass means that it is the inherited copy that belongs to the subclass.

and if the output is (which i dont think so):-

I COME FROM BASECLASS
I COME FROM SUBCLASS

it means the line //3 is executing the version available to the superclass as the method acts on the data available to the superclass.the bottom line is a method can act over data that belongs to its own class.so here we see two methods available to the subclass and i think the former result should be correct.
 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i think is that overriding means :

1. one method will be given preference over the other although both the methods are present.

2. that is why for overriding existance of an inherited method is necessary.

class A{
private void doStuff(){} //method #1
}
class B extends A{
private void doStuff(){} //method #2
}

this is not overriding because method#1 cannot be inherited. and once a method is inherited a virtual copy is maintained in the subclass that wil act on the data belonging to the subclass. and ofcourse there will also be an overriden method. now if we use super the former is called else the overridden method.

this is how i think.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now you are mixing stuff. The super keyword is used to call the super's version of the method. That is why it is called super.

This is needed because there are cases where you want to enhance the base class, but not have to rewrite the base class methods -- Java give you the option to call the "previous latest version" of the method.

Henry
 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right! i agree with you that super is used to call the super version of the method but the one available to the subclass i mean inherited version available to the subclass.

to further strengthen my point, as i have just read in the SCJP book by kathy sierra and bert bates - it clearly said that

[Q]
overriding is "re-implementation" of the inherited method in the subclass"
[/Q]

it means there are two methods available to the subclass 1. inherited(from super class) and the other 2. one belonging to the subclass i.e subclass's version. now when we call super we actually mean (1) i.e. inherited version. now this inherited version would act on subclass data.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!