• 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

Method call

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When compiled & run,The method from the Top class is called ,giving the o/p :"Testing myTop method in Top class".
I thought it would call the method from the Down class,bcos of the reference created.Pl.clarify.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods are not inherited.
as they are not inherited even your TopClassRefVariable is holding object of Down class, it is calling Top class method.
Static methods are not overridden they hidden by there exteding class if extending class has same name method as static.
try to complie Down class after removing static from your method.
HTH
CMIW
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geetha,
The method myTop() is a static method and static method have compile time binding so static method MYTop() from Top class will be called during run time.
But if you remove the static modifier from both the Top and Down class it will print the line from the Down class.
 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation.
Also removed the static keyword and tried to compile & run the code.
Got it.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods can not be overridden. only hidden. What about static method overloading and variable hiding?
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thiru Thangavelu:
Static methods can not be overridden. only hidden. What about static method overloading and variable hiding?


what do you mean ?? static methods are not overloaded/ridden.
Variable hiding is having same member var name in subClass.
plz go refer JLS section 8.4.6 Inheritance, Overriding, and Hiding
HTH
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So is this the conclusion of static modifier?
1) Static methods can not be overridden. It can only be hidden
2) Static methods can not be overloaded at all. If you try, it is compiler error.
3) Static variables are only hidden
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thiru Thangavelu:
2) Static methods can not be overloaded at all. If you try, it is compiler error.

That is incorrect. Static methods can be overloaded.
public static void method1(String s) {}
public static void method1(String s, int i) {}
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are inherited if not hidden:
JLS 8.4.6.2


If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.

 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No argument with Jose and Thomas in this forum..
They are always right. follow them .. and as my signature says "dont beleive me, find it out".
All the best to all
 
reply
    Bookmark Topic Watch Topic
  • New Topic