Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why superclass reference invokes subclass method

 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
this is a code snippet which creates an instance of subclass and get it referred to superclass.why the superclass reference refer to the variable in superclass while it invokes method of subclass?

class Base {

int i=99;

public void amethod(){

System.out.println("Base.amethod()");

}

}



public class RType extends Base{

int i=-1;

public static void main(String argv[]){

Base b = new RType();//<= Note the type

System.out.println(b.i);

b.amethod();

}

public void amethod(){

System.out.println("RType.amethod()");

}



}

the answer is 99 and RType.amethod().why is it not 99 and Base.amethod()
thanks in advance
rajesh
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the phenomeon is call "overriding" a method. if you have a C++ background you should take note that every method is implicitly declare "virtual" except if they are declared final but then nobody can override it.

Julien
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are experiencing is called "polymorphism". In programming languages it's the ability to process objects differently depending on their actual class regardless of the reference object.

When you create a class hiearchy, you can refer to any object in the heiarchy with a reference of any object higher in the heiarchy. When you then use any of the methods or public fields defined in the reference class, the runtime environment begins searching for the method or field in the actual class. If the method or field is not found in the actual class, the environment then moves up the hiearchy one level and checks the parent class for the item. It continues to do this until the method or field is found in one of the ancestor classes and it then uses that item.

This has the effect of always choosing the most specialized version of the method or field available. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results.

Cheers,
[ September 21, 2005: Message edited by: Tom Blough ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the Campfire story How my Dog learned Polymorphism for more information.
 
Manikanta Sabarish
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you make both amethod() in Subclass & SuperClass as static,
the you will get the predicted answer(99 & Base.amethod()),

A static is class-specific and non-static is object-specific.

since your amethod() in both subclass & superclass is non-static(object-specific)
and your object b is of RType ,
The b.amethod(); returns to the RType's amethod().

Tx
[ September 21, 2005: Message edited by: Manikanta Sabarish ]
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u so much Mr.Tom . ur second para of answer is wat dispelled my doubts.thanks 2 u so much & others who replied.
rajesh
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also experience variable hiding as the variable i is hidden by the version in its base class. I think in this case you get the version of which ever reference type you call it on.
 
Manikanta Sabarish
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like you said.
Thank You all & correct me If I am wrong.
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well if both the methods were static then only the superclass version of the method is called.So does it mean for static method the said rule dont apply??
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rajesh chnadra:
well if both the methods were static then only the superclass version of the method is called.So does it mean for static method the said rule dont apply??


Yes, static methods follow different rules than non-static methods. In this situation, polymorphism doesn't work for static methods.

HTH

Layne
 
Marco Vanoli
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ad if i want to print out b.i that is -1?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic