• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

static method hidden ?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source:http://enigma.vm.bytemark.co.uk/webstart.html
John Meyers's SCJP 5 mock exam:question 60



A transient
B default
C private
D Hiding cannot be prevented
E protected
F final
The correct answer is C.
final methods cannot be overriden and private methods are never inherited so they are not overriden either. However option F would cause a compile time error since a final method cannot be overridden.

I thought the answer would be D.This is what I read from the K&B's book(Chap2):
"
Finally,remember that static methods can't be overridden!This doesn't mean they can't be redefined in a subclass,but redefining and overriding aren't the same thing.Let's take a look at an example of a redefined(remember,not overridden),static method:

Running this code produces the output
a a a
"
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gary,

I think this question is a bit fuzzy. Static methods exactly belong to the class where they are declared. And static methods aren't inherited - whether you declare them public or private - so they can't be overridden. It's useless to think about it too much

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

As you have mentioned static methods can't be public or private as they can't be overridden/inherited.
What should be your answer to above question asked.
i think question is testing our encapsulation concept.
encapsulation is used whenver you have to hide the information from others.
Correct me if iam wrong please..
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer should be C, i can say this only from the question point of view, however it is 200% correct that static method can not be overridden.

let see the question, it says that :

What class modifier(s) can be inserted at line 1 to prevent method() from being overridden (hidden ) without causing compile time errors ?

it clearly indicates that, question asking us to compile the source and the only option we have to put something at line 1.
a funny answer could be put two slashes and make that line comment but this is not the option, the other choice we left with option C, make the method private.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE 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 Dinesh Tahiliani:
As you have mentioned static methods can't be public or private as they can't be overridden/inherited.


No, that is not what Marco said. Static methods can be public or private, but they are not inherited. Inheritance and access control are two different things that don't exclude each other.

Marco just said that static methods are not inherited - not if the static method is public, and also not if it is private.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry my mistake in stating, any how thanks for your reply...
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Jesper got me exactly right

But I still think this is not a problem of the right answer. Here the problem is to have the right question or at least a more precise question.

method() of class "sup" simply isn't known to class "test" because static methods aren't inherited. You have to use class sup or a reference to sup to call this method from within class test. So there's no need to make it private to prevent overriding - a concept which doesn't even exist for static methods.

Either I understand this question totally wrong or this question is stupid. Anyway I think there's no guarantee that every certification question you find on the internet is without errors and 100% correct.

Marco
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit confused about terminology right now ... what about this:

Now SubClass.doStuff() is perfectly permissible and will invoke the respective method in SuperClass, thus have the same result as SuperClass.doStuff(). When SubClass is changed to

however, SubClass.doStuff() will have a different result than SuperClass.doStuff() ... What's the appropriate name for the mechanisms in action in this exmple? I take it it's not inheritance and overriding, but what is it then?
[ May 12, 2008: Message edited by: Guido Sautter ]
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guido,

the whole discussion gets more and more confusing

The mechanism you mentioned is "method hiding". Its similar to shadowing. You hide the static method of a super class with a method of the same signature in a sub class. With non-static member methods overriding would take place in your example.

This is the reason why answer C could make a bit of sense. When you make the method in the super class private it's not even visible to the sub class. From that perspective you could perhaps say this prevents method hiding. Obviously you can circumvent any naming conflicts by making everything in your code private. If that's useful is another question But its still confusing that the question also uses the word "overriding".
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marco,

the reason why C is the correct answer is obvious to me, it was just for the names of things ...
 
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic doesn't seem as confusing as it is made.The substance in the statement that "static methods are hidden but not oveeriden" is that static methods absolutely doesn't benefit from dynamic polymorphism or dynamic dispatch. Like For a static method Super sup = new Sub() doesn't produces effective results which is expected for instance methods.
Static methods resolves this statement to feel like Super sup=new Super().
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic