Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

inheritance and class casting question

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question regarding casting of classes in runtime/compiletime.


class AA {
String s1 = "AA.s1";
String s2 = "AA.s2";

public int show() {
System.out.println("calling method in AA");
return 5;
}
}

class BB extends AA {
String s1 = "BB.s1";

public int show() {
System.out.println("calling method in BB");
return 10;
}


public static void main(String args[]) {
BB x = new BB();
AA y = (AA) x;
System.out.println(y.s1 + " " + y.s2 + y.show());
}
}


It prints the following answer:

calling method in BB
AA.s1 AA.s210

---------------------------


Here casting of x is done during compile time and hence the members of class, AA will be called. But howabout the method, show()? It calls the method in AA and the variables in BB. Just confused on why variables from superclass and methods from subclass.

Any help on explaining the situation would be highly appreciated.

Thanks.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
with regard to ur query, in overriding the variables is been access with respect to the reference of the object and not the content, whereas in case of method it looks at the content of the object to access the relevant object

hence here
BB x = new BB();
AA y = (AA) x;
System.out.println(y.s1 + " " + y.s2 + y.show());

1.
For variable accessing it looks for the reference type ie (AA y ) and not the content.
2.For method accessing it looks at the content of the object ie (AA) x

Hope this helps you
smitha
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,

To implement polymorphism Java uses dynamic method lookup for non-static methods. This means that the method that is actually invoked is determined at runtime according to the type of the object. It will be the method furthest down the hierarchy (i.e. the most specific) that matches the signature. There is no concept of dynamic lookup for fields in Java.

Jules
 
francis joseph
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Smitha and Julian for the great help.

Joe
 
reply
    Bookmark Topic Watch Topic
  • New Topic