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

Dan's Study Guide exam Ch-4 - Overriding static method question

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dan,
This is regarding
Ch-4 Declarations & Access Control
Set 1 - Question 8
It says that the following statement is true

a. If the superclass method is static, then the overriding method must also be static.


But Section 6.2 Pg 181 of Khalid Mughal says that

Any final,static & private methods in a class cannot be overridden.


Is the following method overriding, hiding or something else ?

o/p
Test4Super.m1()
Test4Super.m1()
Test4.m1()
Test4.m1()
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are not overriden, they are hidden. Final methods can't be neither hidden or overriden. Private methods are not inherited in the first place to be hidden or overriden, you can declare methods with the same signature as the private methods in the subclass with no problem.
The "overriden" in the first quote refers to hidding it. A non-static method in a subclass can't have the same signature as a static method in the superclass and vice-versa.
HTH
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the next version of the exam that answer option will be changed to the following.


If the superclass method is static, then any method with the same signature in a subclass must also be static.

 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alfred & Dan !
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Static methods are not overriden, they are hidden. Final methods can't be neither hidden or overriden. Private methods are not inherited in the first place to be hidden or overriden, you can declare methods with the same signature as the private methods in the subclass with no problem.


I am not clear of the difference between
1) overriding
2) hiding and
3) inherit
And also, what do you mean by "same signature" ? method name + parameter type list ? Is the return type of the method included ? No right ? Anything else ?

Any explanation is greatly appreciated.
[ November 22, 2002: Message edited by: Ioow Gneb ]
 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The signature of a method consists of the name of the method and the number and types of formal parameters to the method.
Ref Section 8.4.2 of JLS for method signature.
Section 8.4.6 of JLS will be helpful in knowing the differences b/w inheritance, overriding & hiding.
1) Inheritance - In a class all the non-private methods that are accessible to code from its direct superclass and direct superinterfaces are said to be inherited.
For inheriting the the method of a superclass the class shud not override or hide that method.
2) A method m1 of the class is said to be overridden if the subclass defines a method m1 with the same signature as the superclass's m1.
Overriding is only for instance methods and not for static methods.
3) A method m2 of the class is said to be hidden if m2 is static & the subclass defines a static method m2 with the same signature as the superclass's m2.Hiding is by Static (class methods)
 
Hold that thought. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic