• 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

plz help

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Q2 extends Test
{
static void show()
{
System.out.println("Show method in Q2 class");
}
public static void main(String[] args)
{
Test t = new Test();
t.show();
Q2 q = new Q2();
q.show();
t = q; //what happens here
t.show(); // which method called and why
q =(Q2) t; // why explicit cast
q.show();
}
}
class Test
{
static void show()
{
System.out.println("Show method in Test class");
}
}
plz any body tells me the reason and explaination
thnx

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason you're not getting what you I think you're expecting to get is because of that little word "static". You've just discovered that static methods can't be overridden (ie. it just won't call subclass's versions, as opposed to "final" which will actually cause a compilation error if you try to override it).
So, what's happening is:
t = q;
The reference in q is copied into t, so t and q are now both pointing to the same Q2 object in memory. Note that no casting is required here, since you're "upcasting" - ie. assigning a sub-type to a super-type, which is allowed (like myAnimal = dog).
t.show();
Since Test's show() method is static, Java doesn't look any further down the inheritance tree.
So, what's output is the "in Test class" message.
If the show() methods did NOT have static, then you would get the "in Q2 class" message.
q = (Q2)t;
Cast is needed since you're now "downcasting". OK, we "know" this instance of t is a Q2, but that counts for nothing with the compiler, since at runtime an instance of t may reference ANY sub-type of t (like saying myDog = someAnimal
q.show();
Since show() is defined for Q2, Java doesn't need to go back up the inheritance tree - it can call the method direct.

Hope this helps
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic