File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Override method

 
rex tony
Ranch Hand
Posts: 159
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic