| Author |
Override method
|
rex tony
Ranch Hand
Joined: Aug 29, 2007
Posts: 159
|
|
[code] class Super { public float getNum() { return 3.0f; } } public class Sub extends Super { A. public void getNum() { } B. public void getNum(double d) { } C. public float getNum() { return 4.0f; } D. public double getNum(float d) { return 4.0d; } } [\code] At the Compile time Exception at A.How? [ October 24, 2007: Message edited by: rex tony ]
|
 |
Tarun Chauhan
Greenhorn
Joined: Jul 22, 2007
Posts: 4
|
|
Rex Tony- I think the problem is that you are trying to overload a method with same arguments which is not legal. I tried to compile the code and following is the result.
|
 |
Raphael Kou
Greenhorn
Joined: Oct 23, 2007
Posts: 13
|
|
|
Yes, Answer A tries to override the method and one of the rules says that the return typ must be the same (in this case float) or a subtyp.
|
 |
rex tony
Ranch Hand
Joined: Aug 29, 2007
Posts: 159
|
|
Hello Dzu-I Kou Return type void is sub Type of Float?.If so,Tell me the Hierachy for the return type.
|
 |
Raphael Kou
Greenhorn
Joined: Oct 23, 2007
Posts: 13
|
|
No, "void" is "void" and not a class so it can't be a subclass of any class. So A won't compile!! One of the rules says that when you override the return typ must be the same or a subtype. So lets assume you have this class with the getNum() Method but instead of float the return type is "Number" class Super { public Number getNum() { return 3.0f; } } Now when you override the getNum() method you must write the same typ public Number getNum() { return 3.0f; } or a subtyp of "Number" like Integer public Integer getNum() { return 3.0f; } [ October 24, 2007: Message edited by: Dzu-I Kou ]
|
 |
 |
|
|
subject: Override method
|
|
|