• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this Overriding?

 
Vijay Nimkarde
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Blip
{
protected int blipvert(int x) { return 0; }
}
public class Overload155
{
public static void main(String[] args)
{

System.out.println(new Overload155().blipvert(5));
}
protected String blipvert(int x) { return "welcome"; }
}
In the Overriding methods return type must be same or subtype of return type define in super class then Is this Overriding ??? As it compiles Fine.
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay,
I find no superclass or subclass in your code. So, no overriding is done. The 'blipvert' function of Overload155 class happens to have the same name as that in Blip class. That's it. And, these two classes are no way related to each other through inheritance.
 
Vijay Nimkarde
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry 4 wrong code
class Blip
{
protected int blipvert(int x) { return 0; }
}
public class Overload155 extends Blip
{
public static void main(String[] args)
{

System.out.println(new Overload155().blipvert(5));
}
protected String blipvert(int x) { return "welcome"; }
}
In the Overriding methods return type must be same or subtype of return type define in super class then Is this Overriding or Overloading or neither ???
 
Hendy Setyo Mulyo
Ranch Hand
Posts: 219
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijay Nimkarde:
class Blip
{
protected int blipvert(int x) { return 0; }
}
public class Overload155
{
public static void main(String[] args)
{

System.out.println(new Overload155().blipvert(5));
}
protected String blipvert(int x) { return "welcome"; }
}
In the Overriding methods return type must be same or subtype of return type define in super class then Is this Overriding ??? As it compiles Fine.


Vijay,

Assumed your Overload155 class extends Blip class, you will get compilation error since the return type of Blip.blipvert(int x) is different with Overload155.blipvert(int x). If you want to override a method, just make sure the overridden method has the same return type as that in the superclass'.
[ October 26, 2008: Message edited by: Hendy Setyo Mulyo ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to Hendy's statement:

As of Java 5, the return type of a subclass's overridden method can also be a subtype of the return type of the superclass's method.

Eg: The following code is correct as of Java 5, because String is a subclass of Object. This is called covariant return type.

 
Vijay Nimkarde
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Handy and rekha.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic