aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static method hidden ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static method hidden ?" Watch "static method hidden ?" New topic
Author

static method hidden ?

Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
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
"


SCJP 5.0 72%
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

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
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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..


Thanks<br />Dinesh
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Sorry my mistake in stating, any how thanks for your reply...
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

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
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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
Bartender

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Dec 22, 2004
Posts: 142
Thanks Marco,

the reason why C is the correct answer is obvious to me, it was just for the names of things ...
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

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().


Thanks & Regards
Sidharth Pallai
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static method hidden ?