wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is this Overriding? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is this Overriding? " Watch "Is this Overriding? " New topic
Author

Is this Overriding?

Vijay Nimkarde
Ranch Hand

Joined: Oct 16, 2008
Posts: 50
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.


Java Developer
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
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

Joined: Oct 16, 2008
Posts: 50
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

Joined: Dec 01, 2004
Posts: 219

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 ]

Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
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

Joined: Oct 16, 2008
Posts: 50
thanks Handy and rekha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this Overriding?