aspose file tools*
The moose likes Beginning Java and the fly likes method overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method overriding " Watch "method overriding " New topic
Author

method overriding

Amit Sethi
Ranch Hand

Joined: May 30, 2006
Posts: 45
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

Joined: Apr 14, 2004
Posts: 10336


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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Amit Sethi
Ranch Hand

Joined: May 30, 2006
Posts: 45
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

Joined: Apr 14, 2004
Posts: 10336


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

Joined: Dec 06, 2001
Posts: 3061
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
 
subject: method overriding