简体   繁体   English

java -Parent和Sub类中的静态和实例方法

[英]static and instance method in java -Parent and Sub class

Why cant we declare an instance method in sub Class B which shares the same signature of a static method in parent Class A? 为什么我们不能在子类B中声明一个实例方法,它在父类A中共享静态方法的相同签名?

It throws a compile time error if i try to do that. 如果我尝试这样做,它会抛出编译时错误。

My question is, since static method of parent class is restricted to parent class, why does instance method of child class does not compile. 我的问题是,由于父类的静态方法仅限于父类,为什么子类的实例方法不能编译。

Lets see by code: 让我们看看代码:

` `

public class A{
     static void testStatic(){}
}


public class B extends  A{
      void testStatic (){}
}


public class Test{

public static void main (String[] args){

        A a = new B()

        a.testStatic();

}

` `

In the above code,since A does not have an instance method by that name, and since Java allows static methods to be accessed by objects, Object a of type 'A' pointing to 'B' can call static method present in it(class A). 在上面的代码中,由于A没有该名称的实例方法,并且由于Java允许对象访问静态方法,因此指向'B'的类型'A'的对象a可以调用其中存在的静态方法(类一种)。 But complier throws an error "The instance method cannot override a static method" why? 但编译器抛出错误“实例方法不能覆盖静态方法”为什么?

Note: I can understand if a class does not allow same method name for two methods, even if one is instance and other is static. 注意:我可以理解一个类是否允许两个方法使用相同的方法名称,即使一个是实例而另一个是静态的。 But I fail to understand why it does not allow a sub class to have an instance of same name. 但我不明白为什么它不允许子类具有相同名称的实例。 Especially considering the fact that static methods cannot be overridden. 特别是考虑到静态方法不能被覆盖的事实。 And yet, Java allows subclass to have same name as parent class static method, which is called information hiding, but not overriding. 然而,Java允许子类与父类静态方法具有相同的名称,这种方法称为信息隐藏,但不是覆盖。

The compiler throws an error because those are the rules of the language. 编译器会抛出错误,因为这些是语言的规则。 From the Java Language Specification §8.4.8.2 : Java语言规范§8.4.8.2开始

If a class C declares or inherits a static method m , then m is said to hide any method m' , where the signature of m is a subsignature ( §8.4.2 ) of the signature of m' , in the superclasses and superinterfaces of C that would otherwise be accessible to code in C. 如果类C声明或继承static方法m ,则m被称为隐藏任何方法m' ,其中m的签名是m'的签名的子签名(第8.4.2节 ),在超类和超接口中。否则C中的代码可以访问的C.

It is a compile-time error if a static method hides an instance method. 如果静态方法隐藏实例方法,则为编译时错误。

(emphasis in the original). (重点在原文)。 The language is dense (as in most places in the JLS) but it matches the situation you are describing. 语言是密集的(如JLS中的大多数地方),但它与您描述的情况相匹配。 The JLS doesn't provide a rationale for this rule that I could find on first reading. JLS没有提供我在一读时可以找到的这条规则的理由。 But a little thought about how one might try to make this rule unnecessary shows why it's there. 但是有一点想过如何试图让这条规则变得不必要,这就说明了它为什么存在。

It's illegal in Java. 它在Java中是非法的。 The call to the static method is allowed on an instance as well, so there'd be no way to distinguish which method to call in some cases: 实例上也允许调用静态方法,因此在某些情况下无法区分调用哪个方法:

  A a = new A ();
  B b = new B ();
  A.testStatic (); // ok
  B.testStatic (); // ok
  a.testStatic (); // ok calling static
  b.testStatic (); // undefined. What to call? Based on what?

Last call is the reason why it's not allowed. 最后一次通话是不允许的原因。

Calls to static methods are resolved at compile time itself. static方法的调用在编译时自行解决。 So, they cannot be overridden. 所以,他们不能被覆盖。 And by defining another method with the same signature as the static method, the compiler complains. 通过使用与静态方法相同的签名定义另一个方法,编译器会抱怨。

static method is bound with class whereas instance method is bound with object. static方法与类绑定,而instance方法与object绑定。 Static belongs to class area and instance belongs to heap area. 静态属于类区域,实例属于堆区域。

I am not hundred percent sure but I guess answer as below.

Static method means it can be used without an instance of the the class in which it is defined. 静态方法意味着它可以在没有定义它的类的实例的情况下使用。 Also static method can access only static variables of the class. 静态方法也只能访问类的静态变量。 Now if we override non static method and create an instance of sub class with reference of the super class, compiler will be confused for above two basic functioning of static method. 现在,如果我们覆盖非静态方法并使用超类的引用创建子类的实例,编译器将混淆静态方法的上述两个基本功能。 Please debate if any thing wrong in this. 请讨论是否有任何错误。

4 line showing error. 4行显示错误。 so do this public class B extends A 所以这个公共B类扩展了A.

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

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