简体   繁体   English

超类引用的子类变量?

[英]Subclass Reference by Superclass variable?

When a class extends a class, we can use Super-class reference while assigning memory to the subclass object. 当类扩展一个类时,我们可以在为子类对象分配内存时使用超类引用。 I have understood so far is that it is ok to do so, because a subclass inherits the data of its parent class, but it cannot access the members of the subclass because it is the just the reference, and hence does not know of what additions are done by the child class. 到目前为止我已经理解这样做是可以的,因为子类继承了其父类的数据,但它无法访问子类的成员,因为它只是引用,因此不知道添加了什么由儿童班完成。

My question is when I included method hiding to the above concept, the superclass reference variable started to refer to the child's class function. 我的问题是当我将方法隐藏到上面的概念中时,超类引用变量开始引用子类的类函数。 Why is that ? 这是为什么 ? Why it didnt call it's own method as it is supposed to ? 为什么它没有像它应该的那样调用它自己的方法?

class A{
 void show(){ System.out.print("CLass A. \n"); }
}

class B extends A{
 void show(){System.out.print("Class B. \n");  }
}

class Main{
 public static void main(String[] args){
  A a= new A();
  B b= new B();
  a.show();   // prints Class A
  b.show();   // prints Class B

  A a1= new B();
  a1.show();   // print Class B. why is this ? it should be Class A as per theory? 
 }
}

variables and methods are two different things. 变量和方法是两回事。 Variables stick to their types where as methods get executed run time based on the implementation type provided. 变量坚持其类型,其中方法根据提供的实现类型执行运行时。

Polymorphism . 多态性 Methods bind dynamically and choosen at run time. 方法动态绑定并在运行时选择。 If you ovveride them implementation class, they get executed otherwise the implementation from type class gets execute. 如果你实现了它们的实现类,那么它们会被执行,否则类型类的实现会被执行。

When you write 当你写作

 A a1= new B();

Means that please call the implementations from the class B (which is on right side) which are from type A 意味着please call the implementations from the class B来自类型A please call the implementations from the class B (右侧) A

You have to know about overriding concept in java. 您必须了解java中的覆盖概念。

From oracle documentation page regarding overriding: 从oracle 文档页面有关覆盖:

Overriding and Hiding Methods 覆盖和隐藏方法

Instance Methods 实例方法

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method 一个instance method在具有相同签名(名称,再加上数量及其参数的类型)的子类和返回类型的超类的实例方法重写父类的方法

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. 子类覆盖方法的能力允许类从行为“足够接近”的超类继承,然后根据需要修改行为。

But overriding is different from hiding. 但是压倒不同于隐藏。

Static Methods 静态方法

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass. 如果子类定义的静态方法与超类中的静态方法具有相同的签名,则子类中的方法会隐藏超类中的方法。

The distinction between hiding a static method and overriding an instance method has important implications: 隐藏静态方法和覆盖实例方法之间的区别具有重要意义:

  1. The version of the overridden instance method that gets invoked is the one in the subclass. 被调用的重写实例方法的版本是子类中的版本。
  2. The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass. 被调用的隐藏静态方法的版本取决于它是从超类还是从子类调用的。

Example to understand: 要了解的示例:

public class Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Animal");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Animal");
    }
}

public class Cat extends Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Cat");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Cat");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = myCat;
        Animal.testClassMethod();
        myAnimal.testInstanceMethod();
    }
}

output: 输出:

The static method in Animal
The instance method in Cat

它始终从最具体的类中调用该方法。

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

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