*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why does this not compile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why does this not compile?" Watch "why does this not compile?" New topic
Author

why does this not compile?

Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
class SuperClass{
public float getNumericValue(){
return 9.5f;
}
}

public class SubClass extends SuperClass{
public void getNumericValue(){
}
}
It says that I cannot override the method getNumericValue, why is that? Thanks!
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
If one method declaration overrides another, a compile-time error occurs if they have different return types.
In your example, one method declaration has return type float, the other has return type void.
: getNumericValue() in SubClass cannot override getNumericValue() in SuperClass; attempting to use incompatible return type
found : void
required: float
[ December 26, 2003: Message edited by: Marlene Miller ]
Bojan Knezovic
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Originally posted by Michael Sullivan:
class SuperClass{
public float getNumericValue(){
return 9.5f;
}
}

public class SubClass extends SuperClass{
public void getNumericValue(){
}
}
It says that I cannot override the method getNumericValue, why is that? Thanks!


Because you're not overriding you're overloading - the methos in subclass needs to return float.
Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
both, so far I have not received a complete answer. If I do return float, and include a return statement inside the braces, it does compile... which satisfies your answers. However, Have a look at the code below, which compiles just fine:
public class sub extends Super{
public void getNum(double d){ }
}
you can see that I return void, and accept a parameter, and yet it compiles fine. Why?
Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
and this compiles fine as well:
public class sub extends Super{
public void getNum(boolean b){ }
}
Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
so my question still stands, with this code:
public class SubClass extends SuperClass{
public void getNumericValue(){
}
}
why can I not overload the getNumericValue method with a new method that accepts no parameters, and returns void?
Thanks,
Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
one more question, while we are at it. This does not compile:
public class sub extends Super{
public boolean getNum(){return true; }
}

but this does:
public class sub extends Super{
public boolean getNum(boolean b){return true; }
}
as you can see, all I've done is pass in a parameter. Obviously I need a nice condense explanation of overriding methods... becuase this doesn't seem to make sense to me.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Originally posted by Michael Sullivan:

Obviously I need a nice condense explanation of overriding methods... becuase this doesn't seem to make sense to me.

I suggest that you read JLS 8.4.6 Inheritance, Overriding, and Hiding, JLS 8.4.7 Overloading, and JLS 8.4.8 Examples of Method Declarations.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
  • overriding : same method name, same parameter types; instance methods
  • overloading: same method name, different parameter types


  • 1A. If a method declaration overrides the declaration of another method, then a compile-time error occurs if they have different return types or if one has a return type and the other is void.
    2. If a method name is overloaded, the method declarations may have different return types.
  • hiding: same method name, same parameter types, but static methods
  • 1B. If a method declaration hides the declaration of another method, then a compile-time error occurs if they have different return types or if one has a return type and the other is void.
    [ December 27, 2003: Message edited by: Marlene Miller ]
    Vicken Karaoghlanian
    Ranch Hand

    Joined: Jul 21, 2003
    Posts: 522
    Hi Michael and welcome to the ranch, it seems to me you are confused between Overloading and Overriding.
    To successfully override/overload two methods these rules should be applied:
    1) Overloading: The two methods must have the same name, and must have different parameters, the return type of both methods may or may not be the same.
    2) Overriding: The two methods must have the same name, the same parameters, AND also the same return type.
    Now, let get back to your code

    This doesn't compiler because it does not pass the overloading/overriding rules, why? Simple, let us suppose that both methods are overriding, in this case they pass the 'same name' 'same parameter' test however they fail in the return type test (they should both have the same return type but they don't, one of them is float and the other is void).
    Let us try the overloading test, both have the same name... fine, different return type... fine also, however they have the same parameter list which conflict with the overloading rule.
    Now since they are not overloading or overriding each other then it is definitely a compiler time error.

    Hope this helps.


    - Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
    Vicken Karaoghlanian
    Ranch Hand

    Joined: Jul 21, 2003
    Posts: 522
    Gee MM, you beat me on this one with a 4 Nicely explained though.
    [ December 27, 2003: Message edited by: Vicken Karaoghlanian ]
    Marlene Miller
    Ranch Hand

    Joined: Mar 05, 2003
    Posts: 1391
    Vicken, it's a tie. But yours is more friendly than mine. And more like a conversation. Much better. I was trying to be "condensed".
    [ December 27, 2003: Message edited by: Marlene Miller ]
    Michael Sullivan
    Ranch Hand

    Joined: Dec 26, 2003
    Posts: 235
    Thanks all (especially MM, VK, and Marlene) for your comments. After my own research, and re-reading your posts... here are the REALLY simple rules for overriding and overloading methods:
    Overriding methods: Must have the same name, same return type, and same parameters as original method. May not have a more restrictive access modifier than original method (*important, I'm seeing this on practice tests).
    Overloading methods: Must have different parameters, and same name as original method. May have different access level, return type.
    [ December 28, 2003: Message edited by: Michael Sullivan ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: why does this not compile?