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

method overriding

 
Amit Sethi
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1. It was written in K&B Pg. no.102

rules for overriding a method:

"return type must be the same as, or a subtype of, the return type declared in the orignal overridden method in super class"

do this return type applied to primitives or non primitives ?

(I have tried it with primitives it is not working only working on same return type )

can any body please explain me what the author is trying to say here ?

Thanks in advance.
regards
Amit Sethi
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

do this return type applied to primitives or non primitives ?

(I have tried it with primitives it is not working only working on same return type )

How do you make a subtype of a primitive? Primitives are not objects.
[ June 20, 2006: Message edited by: Paul Sturrock ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post some example code that illustrates what you have tried. Also post any compiler error messages that you get, if any. I will be happy to explain what is happening from there (unless someone else beats me to it).

Layne
 
Amit Sethi
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Animal {
long eat() {
System.out.println("i am in class Animal");
return(0);
}
}

class Horse extends Animal {
int eat() {
System.out.println("i am in horse");
return(0);
}
}

class Pg100 {
public static void main(String[] args) {
Animal a = new Animal();
Animal b = new Horse();
long x = a.eat();
int y = b.eat();
}
}
output:
F:\j5se\kathy>javac Pg100.java
Pg100.java:9: eat() in Horse cannot override eat() in Animal; attempting to use incompatible return type
found : int
required: long
int eat() {
^
Pg100.java:20: possible loss of precision
found : long
required: int
int y = b.eat();
^
2 errors


Here return type is long in Animal class & int in Horse class, and both are integeral type & what i think is int is subtype of long.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here return type is long in Animal class & int in Horse class, and both are integeral type & what i think is int is subtype of long.


You think wrong. Primitives don't follow the same rules as objects.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Sethi:
[Here return type is long in Animal class & int in Horse class, and both are integeral type & what i think is int is subtype of long.[/QB]


The term "subtype" only applies to reference variables. Primitive types do not follow any inheritence hierarchy and therefore there are no subtypes. This means that when you use a primitive type as the return type, it must be EXACTLY the same when you override the method in a subclass.

Before version 1.5, class return types also behaved this way. However, as of Java 1.5, you can now use a subclass of the original return type as the return type in an overriding method.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic