aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQ+ question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQ+ question" Watch "JQ+ question" New topic
Author

JQ+ question

amatya
Greenhorn

Joined: Jan 17, 2002
Posts: 4
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

Joined: Jan 17, 2002
Posts: 4
the right answer is 1 and 3. the question is: why not 1 and 5?
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3314
    
    8
You are correct. This question should be fixed.
Can you please tell the question id so that I can fix it.
Paul.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
amatya
Greenhorn

Joined: Jan 17, 2002
Posts: 4
The question ID is: 970935900390
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
amatya,
we'd like you to read the Javaranch Naming Policy and register again.
Thank you for your cooperation


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
ersin eser
Ranch Hand

Joined: Feb 22, 2001
Posts: 1072
Please, read the Javaranch Naming Policy and register again.
Thank you
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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
SCJP 1.4
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
btw, what test is this question from?
Rob
sanz nsgha
Ranch Hand

Joined: Jan 18, 2002
Posts: 42
from jq+
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
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 ]

Regards,
Jim
SCJP, SCJD, SCWCD, SCEA Part I
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
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

Joined: Nov 09, 2001
Posts: 171
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 ]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
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...


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Jim Bertorelli
Ranch Hand

Joined: Nov 28, 2001
Posts: 136
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

Joined: Nov 09, 2001
Posts: 171
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

Joined: Jan 11, 2002
Posts: 203
Proof of the pudding is in the eating:
Try these two programs.

and

Hope this helps
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Nov 09, 2001
Posts: 171
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

Joined: Oct 15, 2001
Posts: 5371
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
Wagner Danda
Greenhorn

Joined: Jul 10, 2001
Posts: 26
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?


Wagner Danda<br />Sun Certified Programmer for Java 2 Platform
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jul 10, 2001
Posts: 26
IBM jikes 1.14
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Nov 09, 2001
Posts: 171
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JQ+ question