Forums Register Login

JQ+ question

+Pie Number of slices to send: Send
Which of the following method definitions will prevent overriding of that method? (Select 2 right answers)
public final void method m1() //1
public static void method m1() //2
public static final void method m1() //3
public abstract void method m1() //4
private void method m1() //5
Right answer: 1 and 5.
Why not 1 and 3?
Another comment: the question itself looks wrong. you cannot declare a "public void method m1()" can you? Shouldn't it be simply "public void m1()"???
+Pie Number of slices to send: Send
the right answer is 1 and 3. the question is: why not 1 and 5?
+Pie Number of slices to send: Send
You are correct. This question should be fixed.
Can you please tell the question id so that I can fix it.
Paul.
+Pie Number of slices to send: Send
The question ID is: 970935900390
+Pie Number of slices to send: Send
amatya,
we'd like you to read the Javaranch Naming Policy and register again.
Thank you for your cooperation
+Pie Number of slices to send: Send
Please, read the Javaranch Naming Policy and register again.
Thank you
+Pie Number of slices to send: Send
 

Originally posted by amatya:
Which of the following method definitions will prevent overriding of that method? (Select 2 right answers)
public final void method m1() //1
public static void method m1() //2
public static final void method m1() //3
public abstract void method m1() //4
private void method m1() //5
Right answer: 1 and 5.
Why not 1 and 3?
Another comment: the question itself looks wrong. you cannot declare a "public void method m1()" can you? Shouldn't it be simply "public void m1()"???


Yes, as written none of these would compile, so none can be overridden. Since this doesn't seem like the intent though, I'll re-write them:


public final void methodM1() //1
public static void methodM1() //2
public static final void methodM1() //3
public abstract void methodM1() //4
private void methodM1() //5


Now these at least make syntactic sense. As for the answers to the original question:
1 is correct, because you cannot override a final method
2 is correct, because you cannot override a static method. (Static methods can be hidden, but not overridden.)
3 is correct, because the method is both final and static and therefore cannot be overriden
5 is correct because private methods in a superclass are not inherited by the subclass; since they're not inherited, they cannot be overriden.
So the only declaration that can be overriden is 4. The answer to the question "Which of the following method definitions will prevent overriding of that method? " is 1,2,3 and 5.
Rob
[ January 17, 2002: Message edited by: Rob Ross ]
+Pie Number of slices to send: Send
btw, what test is this question from?
Rob
+Pie Number of slices to send: Send
from jq+
+Pie Number of slices to send: Send
 

Originally posted by Rob Ross:

Now these at least make syntactic sense. As for the answers to the original question:
1 is correct, because you cannot override a final method
2 is correct, because you cannot override a static method. (Static methods can be hidden, but not overridden.)
3 is correct, because the method is both final and static and therefore cannot be overriden
5 is correct because private methods in a superclass are not inherited by the subclass; since they're not inherited, they cannot be overriden.
So the only declaration that can be overriden is 4. The answer to the question "Which of the following method definitions will prevent overriding of that method? " is 1,2,3 and 5.
Rob
[ January 17, 2002: Message edited by: Rob Ross ]


With all due respect, your answers are still wrong. Static methods may be overriden if they are overridden by a static method. I bought JQ+ and took all the tests and ran into confusion when I took the RHE test and they stated static methods could be overridden. So, I settled it once and for all in my mind and wrote some code in which I did in fact override static methods. Based on the code I wrote and the behavior of the compiler, the following is true:
1. Static methods can override static methods.
2. Non-static methods can override non-static methods.
3. Static methods cannot override static non-methods.
4. Non-static methods can override static methods.
[ January 21, 2002: Message edited by: Jim Bedenbaugh ]
+Pie Number of slices to send: Send
Jim :
Repeat after me
"Static methods can not be overridden. They only hide the static methods in the superclass."
You certainly can create a static method in a subclass, with the same name, signature and return type as the parent class static method. BUT you can NOT then call the parent's static method as super.staticMethod() from your subclass.
Any non static, non private methods in the super class can always be called in the subclass as
super.nonStaticMethod() etc. That is the difference between overriding and hiding.
Hope this helps.
Now , EVERYBODY repeat after me
"Static methods can not be overridden. They only hide the static methods in the superclass."
+Pie Number of slices to send: Send
 

Originally posted by Shivaji Marathe:
Jim :
Repeat after me
"Static methods can not be overridden. They only hide the static methods in the superclass."
You certainly can create a static method in a subclass, with the same name, signature and return type as the parent class static method. BUT you can NOT then call the parent's static method as super.staticMethod() from your subclass.
Any non static, non private methods in the super class can always be called in the subclass as
super.nonStaticMethod() etc. That is the difference between overriding and hiding.
Hope this helps.
Now , EVERYBODY repeat after me
"Static methods can not be overridden. They only hide the static methods in the superclass."


Again, with all due respect, while you may not be able to call the super static method, the defintion given by Sun of an overridden method is one with the same name, arguments and return type. Hence, by this definition and example given, static methods are overridden.
- - - -Late correction- - - -
The JLS states the following in Section 8.4.6.2:
A hidden method can be accessed by using a qualified name or by using a method invocation expression (�15.12) that contains the keyword super or a cast to a superclass type. In this respect, hiding of methods is similar to hiding of fields.
Accordingly, it would appear that your assertion that a hidden method cannot be accessed is incorrect. Please advise if I have interpreted this incorrectly.
[ January 21, 2002: Message edited by: Jim Bedenbaugh ]
+Pie Number of slices to send: Send
 

Originally posted by Rob Ross:

1 is correct, because you cannot override a final method
2 is correct, because you cannot override a static method. (Static methods can be hidden, but not overridden.)
3 is correct, because the method is both final and static and therefore cannot be overriden
5 is correct because private methods in a superclass are not inherited by the subclass; since they're not inherited, they cannot be overriden.


That's what i think too...
+Pie Number of slices to send: Send
Hi Jim,
Think about what overriding actually means. The concept of Overriding applies to objects and not to classes. While static methods are tied to the class and not to the objects of that class. When you call an instance method, you expect the method to work on whatever the actual object is at runtime.
Static methods are a non object oriented feature that just helps you write "functions" (as opposed to "methods"). OO concepts (including overriding) don't make sense for static methods.
HTH,
Jim.
+Pie Number of slices to send: Send
 

Although I don't want to offend anyone, I really don't trust alot of what folks write in forums and on web pages - I've run into alot of mistakes - especially in the mock tests.


That equation doesn't hold here at Javaranch

I've been studying for the exam for over 5 months - I could pass it easily right now with no problem, but I'm trying to get a perfect score.


Good spirit !! You'll get it
[ January 21, 2002: Message edited by: Valentin Crettaz ]
+Pie Number of slices to send: Send
Proof of the pudding is in the eating:
Try these two programs.

and

Hope this helps
+Pie Number of slices to send: Send
To Jim Bedenbaugh,
I'm really sorry I wanted to reply to your message but I edited it instead of quoting it (the buttons are just next to each other...)
I'm REALLY REALLY sorry, could you please post your message again...
I dunno what to say... except SORRY AGAIN...
+Pie Number of slices to send: Send
 

Originally posted by Valentin Crettaz:
To Jim Bedenbaugh,
I'm really sorry I wanted to reply to your message but I edited it instead of quoting it (the buttons are just next to each other...)
I'm REALLY REALLY sorry, could you please post your message again...
I dunno what to say... except SORRY AGAIN...


No problem. Basically, all I said was that although I understand static methods are hidden, sematically and lexigraphically they are no different than overriden methods - I understand that static methods are not "OO" code, but the real confunsion I encountered was a difference I encountered between the RHE and JQ+ tests. Lastly, I didn't want anyone to be offended if I challenged their assertions (unless they provide proper attribution from the JLS or some other authoritative source). I have learned that a great number of things I have read in forums and on web pages are incorrect.
+Pie Number of slices to send: Send
 

Originally posted by Jim Bedenbaugh:

I have learned that a great number of things I have read in forums and on web pages are incorrect.


Yes this is true ..
I luv ranch thats why .. if it is possible then some one from bartenders should make sure that this thing should not happen
as if I have not read Maha's thread then I would have wrong information.
Yes this is true that is also available here only
hope will be taken in good spirit
+Pie Number of slices to send: Send
 

Originally posted by Shivaji Marathe:
Proof of the pudding is in the eating:
Try these two programs.

and

Hope this helps



I can�t compile this code... it says:
*** Error: A "super" expression may only appear in the body of a class that has a super class and it must be enclosed in the body of an instance method or constructor or in the initializer of an instance variable.
What�s happening?
+Pie Number of slices to send: Send
why you cannot compile is because the call to super is inside a static method. that is incorrect.
super and this calls can be made only inside
instance method
i think the author of the code was trying to give an example where he wanted to show how static method and instance methods differ when using the principles of overriding and shadowing.
basically between static methods ie one in subclass and the other in its super-class, the principle of overriding does not hold good.
overriding is only for and between instance methods.
BTW what is your compiler ?

Originally posted by Wagner Danda:

and

Hope this helps<hr></blockquote>

I can�t compile this code... it says:
*** Error: A "super" expression may only appear in the body of a class that has a super class and it must be enclosed in the body of an instance method or constructor or in the initializer of an instance variable.
What�s happening? [/QB]

 
+Pie Number of slices to send: Send
IBM jikes 1.14
+Pie Number of slices to send: Send
 

Originally posted by Jim Bedenbaugh:

No problem. Basically, all I said was that although I understand static methods are hidden, sematically and lexigraphically they are no different than overriden


This is like saying "I know the earth is round, but visually it is no different than being flat, therefore it is the same as being flat."
Your argument isn't logical.

I have learned that a great number of things I have read in forums and on web pages are incorrect.



Yes, like your assertions that static methods can be overriden. You're incorrect.

Take a look at JLS 8.4.6.2 Hiding (by Class Methods).


If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class.


I'm not making this stuff up Jim, it's in the JLS!
Rob
+Pie Number of slices to send: Send
Shivaji,
I understand what you were trying to show. Here's a little simpler example that proves the point:

As you can see, line 1 works as expected. You are making a method call on an SuperClass object reference variable, but because the actual instance is of type SubClass, the overridden method gets called.
In line 2, when we attempt to do the same thing with a static method, we see that the same rules do not apply. The static method that gets invoked is the one that is a member of the reference variable's class, NOT the one that is a member of the actual object instance!
This shows that there is a very real, tangible, important distinction between static and instance methods, and between hiding and overridding. It also shows why it is Bad Form to refer to a static member by an reference variable; you should always use the class name to access a static member. In the above line 2, if you had written
SubClass.staticPrintClassName();
SuperClass.staticPrintClassName();
it would be clear which method would be invoked.

Rob
+Pie Number of slices to send: Send
 

Originally posted by Rob Ross:

This is like saying "I know the earth is round, but visually it is no different than being flat, therefore it is the same as being flat."
Your argument isn't logical.
Rob


You've written a nice example of the Fallacy of the False Analogy. I neither implied nor inferred any such thing, but thanks for your kind comments.
The first person to drink cow's milk. That started off as a dare from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1191 times.
Similar Threads
doubt in K&B book.. pg:135
Confusing SCJP question!
Selftest question
Questions on for Kathy Sierr or Bert Bates
Legal method declarations
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:22:15.