• 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 declaration question

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is
Which of the following statements are true?
a. If an accessible superclass method is static, then any method with the same signature in a subclass must also be static.
b. If a superclass method is synchronized, then the overriding method must also be synchronized.
c. If a superclass method is public, then the overriding method must also be public.
d. If a superclass method is native, then the overriding method must also be native.
e. If a superclass method is protected, then the overriding method must be protected or public.
The answers are a) c) and e)
Option a) is "If an accessible superclass method is static, then any method with the same signature in a subclass must also be static"
I am not able to understand the correctness of option a). Could anyone of you explain this?
What does signature mean? Does it include the return type, method name , paramaters and exceptions.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Lakshmi,

"What does signature mean"


The JLS tells you...

8.4.2 Method Signature
The signature of a method consists of the name of the method and the number and types of formal parameters to the method.


So why is [a] correct? Because an instance method cannot override a static method. And why not? Because the term "override" really does not work here. A static method is a method without an instance context. If you would try to override such a method (without being static), you try to bring in an instance context, which the "overridden" method does not know. So it makes really no sense, because one cannot call it "overriding".
Hope it helps
Detlev
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lakshmi,
I had written the code below, which helped in figure out why static methods are hidden and not overridden. It is a little long, but it was quite useful to me.
Thanks.
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Detlev adn cathy.
Cathy thanks a lot for the code. I understood that Static methods are redefined and not overriden.
I also have another question. is overriding always concerned with Object methods instead of class methods'?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is overriding always concerned with Object methods instead of class methods'?


The basic principle behind overriding is ' A super class variable can refer a sub class object ' . i.e. Dynanic method dispatch - In overriding depends upon the object being referred corresponding method will be invoked. So, it should be concerned with object methods only.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic