简体   繁体   English

在Java中,重载和重写方法之间是否有无人问津?

[英]In Java, is there any no-man's-land between overloaded and overridden methods?

In Java, is there any no-man's-land between overloaded and overridden methods? 在Java中,重载和重写方法之间是否有无人问津? I am trying to find space between "overloaded" and "overridden". 我试图在“超载”和“超载”之间找到空间。

In one scenario, I have placed two methods in a class in order to make them overloaded: 在一种情况下,我在类中放置了两个方法以使其重载:

float my_method(int x, int y) {....}
int my_method(int x) {....}

Then, in another, I have modified the methods and placed them in two different classes with a hierarchical relation, in order to make them overridden: 然后,在另一种方法中,我修改了方法并将它们放置在两个具有层次关系的不同类中,以使它们被覆盖:

void my_method(int x, int y, int z) {....}
void my_method(int x, int y, int z) {....}

What can be done so that the methods written above fall neither in the overloaded nor the overridden category? 如何使上面编写的方法既不属于重载类别也不属于覆盖类别? Is it possible? 可能吗? Or, are the above statements true? 还是上述说法正确?

1. Two methods that fail to be overloaded are automatically overridden
2. Two methods athat fail to be overridden are automatically overloaded

There is a third scenario, in which a method of the sub-class has the same signature as a method of the super-class, but the method of the super-class is not accessible to the sub-class (ie it's either private or package-private and the super-class is in a different package). 第三种情况是,子类的方法与超类的方法具有相同的签名,但是子类无法访问超类的方法(即,它是私有的或私有的)。 package-private和超类在不同的包中)。

In this case the method of the sub-class is not overriding the method of the super-class. 在这种情况下,子类的方法不会覆盖超类的方法。 It just hides it. 它只是将其隐藏。

In super-class : 在超类中:

private void my_method(int x, int y, int z) {....}

In sub-class : 在子类中:

// adding @Override here would cause a compilation error
public void my_method(int x, int y, int z) {....}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM