Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

method overriding

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic