Java inheritance

2019-02-12 08:47发布

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.

5条回答
\"骚年 ilove
2楼-- · 2019-02-12 09:21

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.

查看更多
我只想做你的唯一
3楼-- · 2019-02-12 09:27

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-02-12 09:28

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.

查看更多
倾城 Initia
5楼-- · 2019-02-12 09:36

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.

查看更多
爷的心禁止访问
6楼-- · 2019-02-12 09:42

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.

查看更多
登录 后发表回答