aspose file tools*
The moose likes Beginning Java and the fly likes Why superclass reference invokes subclass method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why superclass reference invokes subclass method" Watch "Why superclass reference invokes subclass method" New topic
Author

Why superclass reference invokes subclass method

Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
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

Joined: Sep 01, 2005
Posts: 41
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

Joined: Jul 31, 2003
Posts: 263
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 ]

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Check out the Campfire story How my Dog learned Polymorphism for more information.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Manikanta Sabarish
Greenhorn

Joined: Jul 18, 2005
Posts: 14
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

Joined: Jun 13, 2005
Posts: 55
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

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Jul 18, 2005
Posts: 14
like you said.
Thank You all & correct me If I am wrong.
Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
Ad if i want to print out b.i that is -1?


bye, <br />Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why superclass reference invokes subclass method