Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding non-static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding non-static method" Watch "overriding non-static method" New topic
Author

overriding non-static method

Wei Du
Greenhorn

Joined: May 03, 2002
Posts: 16
On the Programmer's Final Exam of RHE book, question #40's answer says non-static method can ONLY be overridden by a non-static method.
Of the 4 possible combinations of static and non-static method in parent and child classes, we have
Case 1: parent non-static and child non-static - This is the classic overriding situation; No problem.
Case 2: parent non-static and child static, this is the situation in above question. According to RHE, it's not allowed. But I couldn't find in the book where it was discussed further.
Case 3: parent static and child static, is this the "method hiding case" instead of overriding? Can someone elaborate on this or provide some links?
Case 4: parent static and child non-static, We all know static method can't be overridden. So this is not allowed.
I wish someone could talk about the above Case 2 and Case 3. Thanks.
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1441
Check out section 8.4.6 of the JLS.


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
Originally posted by Wei Du:
Case 3: parent static and child static, is this the "method hiding case" instead of overriding? Can someone

Some people would say that it is not a method overriding but a "method hiding" case. But let's see what the compiler says:
CODE A: Compiles OK
===================
class A {
static void m1(){}
}
public class Java extends A {
static void m1(){}
public static void main(String a[]) {
}
}
CODE B: Unsuccessful OVERRIDE of static method
===============================================
class A {
static void m1(){}
}
public class Java extends A {
static int m1() {}
public static void main(String a[]) {
}
}
G:\JAVA\Class\Java.java:6: m1() in Java cannot override m1() in A; attempting to use incompatible return type
found : int
required: void
static int m1() {}
The error message caused by CODE B reveals that the compiler accepts the valid "overriding" of a static method in CODE A.
In the absence of codes, words could be ambiguous, and we are at the mercy of the intelligence of the exam maker.
[ May 03, 2002: Message edited by: Doanh Nguyen ]
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
The code behaves the same but what really happpends is that the static methods are "Hidden". It's the same thing as overriding, but technically, it's called Hiding. Therefore on questions, technically that's the answer.
Hiding is what happends when you interfere with inheritance, so a static method prevents a class form inheriting a method by hiding.
Shadowing is what happends when you interfere with scope, so a local variable can shadow the member variable that would have been used if the local variable did not have the same name.
Look in the JLS under Hiding, Shadowing, Obscuring.
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
Look in the JLS under Hiding, Shadowing, Obscuring.[/QB]

JLS is an abtract and the Java compiler is its actualization. The contradiction between the two on this issue is obvious but trivial. My message to the original poster was, and I apologize for not able to make it across, don't be too alarmed if you missed questions of this type. (Even the compiler would fail them). These types of questions are best presented by code examples rather than words.
On a side note, every time I see words conveying certainty such as 'always', 'never', 'absolutely' in a mock exam question, I wonder whether the author is aware of such and such scenarios... My favorite type of questions starts with "What is the result of compiling and running this code" :}
Wei Du
Greenhorn

Joined: May 03, 2002
Posts: 16
Thanks to those who responded. I quote JLS:
"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. A compile-time error occurs if a static method hides an instance method".
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The reason that it is "method hiding" and not "overriding" is because "overriding" implies polymorphism. Hidden methods do not participate in polymorphism. Try this:

[ May 06, 2002: Message edited by: Thomas Paul ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding non-static method
 
Similar Threads
classes
static overloading
override static methods.
Static methods cannot be overridden.
Abstract method cannot be static