• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on method overriding

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In KS&BB book,it is written that in method overriding...whatever the superclass declares as a return type,the overriding method must declare either
the SAME type,or a SUBCLASS type..


keeping that in mind..i have overrided the SuperClass method display() by changing its return type..but it is showing error that we cant overide in this way... can ny1 figure out where i have done wrong?



class SuperClass {
public int display(){
System.out.println(" In SuperClass method.");
return 1;
}
}


public class Method_Overriding extends SuperClass{

public short display(){
System.out.println(" In Overriding method.");
return ( (short)1);
}

public static void main(String[] args) {
SuperClass sc1 =new Method_Overriding();
sc1.display();

}
}
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prabhat Gupta:
the overriding method must declare either the SAME type,or a SUBCLASS type..

class SuperClass {
public int display(){
System.out.println(" In SuperClass method.");
return 1;
}
}


public class Method_Overriding extends SuperClass{

public short display(){
System.out.println(" In Overriding method.");
return ( (short)1);
}

public static void main(String[] args) {
SuperClass sc1 =new Method_Overriding();
sc1.display();

}
}



Hi,

your "return objects" should be the same or subclasses, as you mentioned.

try something like that:


Regards,
Alex
[ January 23, 2008: Message edited by: Alex Belisle Turcot ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The return types you have used are int and short.

Please Note int and short are primitives and short is never subclass of int.

You cannot override in the way you have done presently.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

first of all int and short are not Object...so you can not say short is not subclass of int..!!
In Java, I think apart from primitive type everything else is Object..!!

you I would request you to change the return type of both super class and sub class and use some Object and try to clear your clarification..!!!
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnx a lot buddy...
 
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic