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:
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
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..
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.
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.
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 ]
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".
Joined: Dec 22, 2004
the reason why C is the correct answer is obvious to me, it was just for the names of things ...
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().