简体   繁体   English

如果父类和子类具有相同的方法,并且我使用父类的引用来存储子类的对象

[英]If parent and child class are having same methods and i am using the reference of parent class to store the object of child class

Parent class父类

class Animal {
    public void sound() {
        System.out.println("Grr");
    }
}

Child class儿童班

class Dog extends Animal {
    public void sound() {
        System.out.println("Woof");
    }
}

Calling class调用类

class Callings {
    public static void main(String args[]) {
        Animal a = new Dog();
        a.sound();
    }
}

Output is - "Woof"输出是 - “Woof”

My concern is--> I have read till now that if we use the parent class reference and point to object to child class then using the reference of parent class we can only access the data members and methods of the parent class only, if this is true then output of above code should be "Grr".我的担忧是-->我已经读到,如果我们使用父类引用并指向子类的对象,那么使用父类的引用我们只能访问父类的数据成员和方法,如果这样为真,则上述代码的输出应为“Grr”。 But the output is "Woof" can any one explain why.但是输出是“Woof”,任何人都可以解释原因。

Even you are creating and object for Animal but the instance is type of Dog , you can create an instance of a child class and assign to the type of parent.即使您正在为Animal创建和对象但实例类型为Dog ,您也可以创建子类的实例并分配给父类的类型。

When you call a method, it should be defined in the parent class and if it is overridden in child then the child method will be called else the parent's method will be called.当您调用一个方法时,它应该在父类中定义,如果它在子类中被覆盖,则将调用子方法,否则将调用父类的方法。

You can check the type of the instance by printing the class type of the instance a by using System.out.println(a.getClass().getName());您可以通过使用System.out.println(a.getClass().getName());打印实例a的类类型来检查实例的类型System.out.println(a.getClass().getName()); which will print Dog这将打印Dog

Dog knows it is a Dog , even if you call it an Animal . Dog知道它是Dog ,即使您称它为Animal

Check this question for more details.检查此问题以获取更多详细信息。

In java when your child class has a function named the same as the parent class, it overrides it.在java中,当你的子类有一个与父类同名的函数时,它会覆盖它。 In your instance, you have a parent function called "sound".在您的实例中,您有一个名为“声音”的父函数。

public void sound() {
    System.out.println("Grr");
}

When you name the child function "sound" then you override the parent one.当您将子函数命名为“声音”时,您将覆盖父函数。

public void sound() {
    System.out.println("Woof");
}

So every time you override a parent function then the child function will be called.因此,每次覆盖父函数时,都会调用子函数。

暂无
暂无

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

相关问题 为什么引用子类对象的父类类型引用变量无法访问子类的方法 - Why parent class type reference variable having reference to child class object can't access child class's Methods 如何使用子类之外的子引用访问与子变量同名的父类变量? - How to access parent class variable having same name as child variable with child reference outside the child class? 为什么我可以访问父类中的子方法? - Why am I able to access child methods in the parent class? 如果在父类和子类中有两个同名方法。 - if there are two methods with same name in Parent class and Child Class ..? 为什么在父类中必须强制有一个子方法以引用父类的引用来调用它,该父类引用子类的对象? - Why is it mandatory to have a child method in Parent class to call it with a reference of parent class which is referring to an object of child class? 如何通过使用父类的引用访问子类的成员? - How to access members of child class by using reference of parent class? 我可以使用父对象调用子类方法吗? - Can i call child class method using parent object? 如何使用子类引用使用父关键字调用父类方法以创建对象? - How to call a parent class method using child class reference using super keyword for object creation? 从子类访问父类对象 - Access Parent Class Object from a child class 存储在数组中列出同一父类的两个子类 class - Store in Array List two child classes of the same parent class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM