• 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 wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JQ+ question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()"???
 
amatya
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the right answer is 1 and 3. the question is: why not 1 and 5?
 
Enthuware Software Support
Posts: 4906
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. This question should be fixed.
Can you please tell the question id so that I can fix it.
Paul.
 
amatya
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question ID is: 970935900390
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
amatya,
we'd like you to read the Javaranch Naming Policy and register again.
Thank you for your cooperation
 
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, read the Javaranch Naming Policy and register again.
Thank you
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw, what test is this question from?
Rob
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from jq+
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Jim Bedenbaugh
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 5399
1
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 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...
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Bedenbaugh
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Proof of the pudding is in the eating:
Try these two programs.

and

Hope this helps
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Jim Bedenbaugh
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
R K Singh
Ranch Hand
Posts: 5399
1
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 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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]

 
Wagner Danda
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IBM jikes 1.14
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jim Bedenbaugh
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic