Java inheritance

ぃ、小莉子 提交于 2020-01-11 00:57:16

问题


Why does is print last "I'm a Child Class." ?

public class Parent
{
    String parentString;
    public Parent()
    {
        System.out.println("Parent Constructor.");
    }

    public Parent(String myString)
    {
        parentString = myString;
        System.out.println(parentString);
    }

    public void print()
    {
       System.out.println("I'm a Parent Class.");
    }
} 

public class Child extends Parent
{
    public Child() {
        super("From Derived");
        System.out.println("Child Constructor.");
    }

    public void print()
    {
       super.print();
       System.out.println("I'm a Child Class.");
    }

    public static void main(String[] args)
    {
        Child child = new Child();
        child.print();
        ((Parent)child).print();
    }
}

Output:

From Derived

Child Constructor.

I'm a Parent Class.

I'm a Child Class.

I'm a Parent Class.

I'm a Child Class.

回答1:


Because this is an example of polymorphism (late binding). At compile time you specify that the object is of type Parent and therefore can call only methods defined in Parent. But at runtime, when the "binding" happens, the method is called on the object, which is of type Child no matter how it is referenced in the code.

The part that surprises you is why the overriding method should be called at runtime. In Java (unlike C# and C++) all methods are virtual and hence the overriding method is called. See this example to understand the difference.




回答2:


Both:

child.print();

and

((Parent)child).print();

Should return:

I'm a Parent Class.
I'm a Child Class.

In that order.

Casting an object to its base class doesn't change which method call gets made.

In this case, even after the cast, the child print method gets called and not the parent.




回答3:


Even though you cast child to Parent this does not change its implementation of the method. This does not actually make it a parent. This is why you can do something like:

Image img = new BufferedImage(...);

Even though Image is abstract img is still a bufferedImage underneath.




回答4:


Even though you are casting it as the Parent class, that does not mean it will use its parent's functions-- it will only treat the object as a Parent.

By saying ((Parent)child).print(); you are saying "Treat this object as a Parent Object, Not a Child Object". Not "Use the parent method implementations"

Thus if the child object had other methods you wouldn't be able to call them. But since print() is a method of the parent you can still call it, but it uses the actual object's (not the class its casted to) implementation.




回答5:


in java if we call any method on one object then its calls the method by using sub most object ............... here:

Object | | Parent | | Child <- sub most object

Case 1::

Child child = new Child();

    child.print();

this calling child class method print by using child object... child is sub most so it prints

output::

I'm a Parent Class. I'm a Child Class.

Explanation: From child method it is calling parent method because of super.print(); method so first line is I'm a Parent Class. after completion of parent method execution controls comes back to child method and prints I'm a Child Class.

Case 2::

    ((Parent)child).print();
if we call like this jvm calls print () method by using sub most object here sub most  object is child object so it prints 

output::
          I'm a Parent Class.
          I'm a Child Class.

Explanation: From child method it is calling parent method because of super.print(); method so first line is I'm a Parent Class. after completion of parent method execution controls comes back to child method and prints I'm a Child Class.



来源:https://stackoverflow.com/questions/3506412/java-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!