• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt  RSS feed

 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {
public byte getNumber() {
return 1;
}
}

class B extends A {
public short getNumber() { // error
return 2;
}

public static void main(String args[]) {
B b = new B();
System.out.println(b.getNumber());
}
}

\\Error:- creates error it doesnot override the method why is it so.

Please help me to find out the reason
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to hear from you the rules to override??
can you please tell them?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, overloading is not determined or accepted if there is a change in the return type alone!!

If in case of overriding, you need to replicate the exact signature of the method in base class without any change!

HtH.
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:
Because, overloading is not determined or accepted if there is a change in the return type alone!!

If in case of overriding, you need to replicate the exact signature of the method in base class without any change!

Just to point out that, in Java 5, there is an exception to this: a subclass method can declare a return type that is a a subtype of the superclass return type; e.g., the superclass method signature could be:

public Number getNumber() { ... }

a legal subclass override could be:

public Integer getNumber() { ... }
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh.. thats great..

Thanks for pointing out that Matt Russell !

I am not aware of this fact and am still with JDK 1.4 only
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found that for primitives, the overriding method must return the exact same type as the method that is being overridden.

The new feature in Java 5 allowing you to return a subclass as the result of an overriding method only works for objects.

This limitation on the return type of primitives can be worked around by using the wrapper types if necessary.

Kaydell
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kaydell,


This limitation on the return type of primitives can be worked around by using the wrapper types if necessary.


I think thats what Matt had also said by giving java.lang.Number and java.lang.Integer in the example.

Am i right Matt?
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:

I think thats what Matt had also said by giving java.lang.Number and java.lang.Integer in the example.

Am i right Matt?

Unfortunately, the number wrapper classes don't inherit from each other (so, Byte doesn't extend Short etc).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!