Is that Overload or Override?
public class overload1 {
public Object show()
{
return "0";
}
}
cl开发者_运维知识库ass overload2 extends overload1{
public String show(){
return "1";
}
}
Override. You are extending the class, and overriding the superclass method.
Overload would look like this:
public class overload1 {
public Object show()
{
return "0";
}
public Object show(Object arg) // Same method name, but different arguments.
{
return "0";
}
}
Easiest way to test is to put @Override
:
class overload2 extends overload1{
@Override
public String show(){
return "1";
}
}
So it is a valid override (considering Java > 1.5)
Method overloading means two or more methods has the same name but different signatures(i.e. different input parameters type , different return type)
Method overriding means having a different implementation of the same method in the inherited class. These two methods would have the same signature, but different implementation.
So , in your example , overload2 class 's show() override the overload1 class 's show()
What you are doing is overriding. Overloading is when 2 methods have the same name but different parameters.
Hope it helps :)
Method Overloading means writing same method name in same class with different of arguments and overriding means writing same method in super class and sub class.
In your example you are writing same method in super class and sub class so it's method overriding.
It's an Overriding cause you just redefined the method show() that has already been defined in a parent class overload1. Overloading deals with multiple methods with same methods name, in the same class, but with different parameters.
it is a type of Overriding because in overloading parameter must be change
That's an override. See Tom Jeffery's answer for an overload.
I think what you might be asking is "Can you overload by changing the return type?" You cannot do that. You need to change the method signature (parameter type list) in order to overload. You can do this in the same class or in subclasses.
An override means that a method in a superclass is not reachable any more. In other words, by calling .show()
on an instance of overload2
, can you directly reach overload1.show()
? If not, it's an override.
精彩评论