Java 子类中的方法可以重载超类中的方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24884151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:21:12  来源:igfitidea点击:

Can a method in sub class overloading a method in super class?

javaoverloading

提问by Freewind

Java code:

爪哇代码:

class P {
    public void hello() {}
}

class C extends P {
    public void hello(String s) {}
}

My question is: Is the helloin class Coverloading the one with same name in super class P?

我的问题是:hello类中是否C重载了超类中同名的类P

My friend says they are not because the are not in the same class.

我的朋友说他们不是,因为他们不在同一个班级。

采纳答案by JamesB

Taking a more formal approach, the Java Language Specification for Java 7 states:

Java 7 的 Java 语言规范采用更正式的方法指出:

If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

如果一个类的两个方法(无论是在同一个类中声明,还是都被一个类继承,或者一个声明一个继承)具有相同的名称但签名不是覆盖等效的,那么该方法名称被称为超载。

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.9

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.9

I would point your friend to this link.

我会把你的朋友指向这个链接。

So, in short, in your example, the hello method is indeed overloaded.

因此,简而言之,在您的示例中, hello 方法确实重载了。

回答by Zoltán

Long story short, an instance of Cwill have both the hello()and the hello(String s)methods available. An instance of Pwill only have the hellomethod available.

长话短说,一个实例C将同时具有hello()hello(String s)方法。的实例P将只具有hello可用的方法。

This is indeed overloading, as you have two methods of the same name taking different parameters.

这确实是重载,因为您有两个采用不同参数的同名方法。

However, it is not overriding, because overriding is having a method declared in a subclass with the same name and same parametersas a method in a superclass.

但是,它不是覆盖,因为覆盖是在子类中声明的方法与超类中的方法具有相同的名称和相同的参数

E.g. if you had

例如,如果你有

class C extends P {
    public void hello() {}
}

it would be overridingthe hello()method declared in P. When invoking new C().hello()in that case, you would invoke the implementation of the hello()method declared in class C.

它将覆盖hello()声明的方法Pnew C().hello()在这种情况下调用时,您将调用hello()class 中声明的方法的实现C

回答by nobalG

Yes it is overloading, This overloading is happening in case of the class 'C' which is extending Pand hence having two methods with the same name but different parametersleading to overloading of method hello()in Class C. However Class Pis only able to access one of the methods which is present in its own definition.

Yes it is overloading, 这种重载发生在类 ' C' 正在扩展P并因此具有two methods with the same name 但different parameters导致方法hello()in重载的情况下Class C。但是Class P,只能访问其自身定义中存在的方法之一。

回答by Maroun

Overloadingis when you have two methods that have the samename but different signatures (Your case).

重载是指您有两个名称相同但签名不同的方法(您的情况)。

Side note: Overridingis when you have two methods that have exactlythe same signature and name and the parent class.

旁注:覆盖是当您有两个具有完全相同的签名和名称以及父类的方法时。

回答by f1sh

It is a valid question since usually, overloading is explained using two methods with the same name (but different parameters) in the same class.

这是一个有效的问题,因为通常,重载是使用同一类中具有相同名称(但参数不同)的两个方法来解释的。

I would argue that yes, the method helloin Cis overloading P's hellomethod because of the "is a" relation.

我认为是的,由于“是一个”关系,helloin 中的方法C是重载Phello方法。

The "is a" relation states that since C subclasses P, it is also an instance of P ("C is aP"). Hence C has 2 overloaded hello-methods.

“是一个”关系表明,由于 C 是 P 的子类,因此它也是 P 的一个实例(“CP”)。因此 C 有 2 个重载的hello方法。

回答by Niru

Overloading can happen in same class as well as parent-child class relationship whereas overriding happens only in an inheritance relationship.

重载可以发生在同一个类以及父子类关系中,而覆盖仅发生在继承关系中。

回答by Jatin Lalwani

Simple Explanation:

简单说明:

I think this question arises because at times we hear the following,

我认为出现这个问题是因为有时我们会听到以下内容,

"Method overloading is performed within class. Method overriding occurs in two classes that have inheritance relationship."

"方法重载在类内进行。方法覆盖发生在两个有继承关系的类中。"

The above statement is correct. But your friend is wrong. why?

上述说法是正确的。但是你的朋友错了。为什么?

Because when you extend a class, the subclass have all the methods defined by superclass. It is as if all the methods of superclass have been implemented by the subclass.That means the hello() method has been implemented by the class C as well. Now, you added a method in class C with different parameter (hello(String s)). That means, class C has two methods in all with same name but different parameters and that is "overloading".

因为当你扩展一个类时,子类拥有超类定义的所有方法。就好像超类的所有方法都被子类实现了。这意味着 hello() 方法也已由类 C 实现。现在,您在类 C 中添加了一个具有不同参数(hello(String s))的方法。这意味着,C 类共有两个名称相同但参数不同的方法,即“重载”。

Hope it is crystal clear.

希望它是透明的。

回答by pravin shinde

Yes, your friend is wrong because he thinks only of the concept of overriding.

是的,你的朋友错了,因为他只考虑覆盖的概念。

But here hello(), and hello(String s)are different by there parameters so it's overloading not overriding.

但是这里 hello(), 和hello(String s)那里的参数不同,所以它是重载而不是覆盖。

回答by Abhinav

Good question!!!In sub class if method name | parameter type | list is changed then sub class method will not be considered as overriding it is considered as overloading method Example :

好问题!!!在子类中 if 方法名称 | 参数类型 | 列表更改然后子类方法不会被视为覆盖它被视为重载方法示例:

class A{
void m1(int a){}
}

class B extends A{
  void m1(float f)
   {}
}

In above program m1 method is a overloaded method.

在上面的程序中 m1 方法是一个重载方法。

回答by Sonu patel

Yes we can overload the super class method in sub class like as bellow:

是的,我们可以在子类中重载超类方法,如下所示:

    public class OverLoading {

    public static void main(String[] args) {
        B b = new B();
        b.display();
        b.display(4);
    }
}


class A {
    public void display() {
        System.out.println("A class display method");
    }
}

class B extends A {
    public void display() {
        System.out.println("class B subclass");
    }

    public void display(int a) { //Overloading in subclass
        System.out.println("class B subclass with overloading");
    }
}
Output: 
class B subclass
class B subclass with overloading