Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why does this not compile?

 
Michael Sullivan
Ranch Hand
Posts: 235
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 235
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 235
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and this compiles fine as well:
public class sub extends Super{
public void getNum(boolean b){ }
}
 
Michael Sullivan
Ranch Hand
Posts: 235
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 235
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • 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
    Posts: 522
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Vicken Karaoghlanian
    Ranch Hand
    Posts: 522
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1392
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 235
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic