Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why superclass reference invokes subclass method

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the Campfire story How my Dog learned Polymorphism for more information.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ad if i want to print out b.i that is -1?
 
I will suppress my every urge. But not this shameless plug:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic