Java method access on runtime polymorphism

java oop polymorphism dynamic-dispatch

92 观看

5回复

595 作者的声誉

I have the following java code.

class A {

        public void method1() {
            System.out.println("A 1");
            method2();
        }

        public void method2() {
            System.out.println("A 2");
        }

    }

    class B extends A {
        @Override
        public void method2() {
            System.out.println("B 2");
        }
    }

    public class Tester {
        public static void main(String[] args) {
            A a = new B();
            a.method1();
        }
    }

It prints

    A 1
    B 2
  • What exactly happens at runtime when a.method1() is called?
  • How is the derived method getting called from the parent?
  • Is it looking at the object and the method name string and calling the method during runtime?
  • Is it calling this.method2() by default?
作者: Sharuk Ahmed 的来源 发布者: 2017 年 12 月 27 日

回应 5


0

107198 作者的声誉

What exactly happens at runtime when a.method1() is called?

method1() of Class A called.

How is the derived method getting called from the parent?

Since you have ovverided the method, method2 in class B, that will be the method which is going to be called at runtime.

Is it looking at the object and the method name string and calling the method during runtime?

Don't get confuse here. When you write

 A a = new B();

Methods from Class B gets called if they are ovveridden otherwise from A gets called.

Is it calling this.method2() by default?

Again, it is not defaulting. If you ovveride it gets called from B. If you don't ovveride, it gets called from A

作者: Suresh Atta 发布者: 2017 年 12 月 27 日

3

58526 作者的声誉

决定

Since the method method1(...) was never overridden, B inherits A's method1() and it is called as if it were defined in B.

Since method1() calls method2() the overridden method method2() defined in B is called, when the instance was created with the B constructor.

If you create another instance, with the A constructor, you will not get the overridden method2(...) defined in B, but get the original method2(...) defined in A.

作者: Edwin Buck 发布者: 2017 年 12 月 27 日

0

8194 作者的声誉

Because your object is actually of type B that is inheriting from A. inside your B's class you're overriding method2 that is why it is calling B.method2()

  A a = new B();

To call method2 from A, use:

  A a = new A();
作者: Muhammad Soliman 发布者: 2017 年 12 月 27 日

0

1 作者的声誉

When object of type B is initialized it is an instance of class B even though it is implicitly cast to an object of type A.

Hence when you call a.method1() since method1() is not defined in class B it calls the method defined in the base class. Then in turn when method2() is called it is defined in class B and since the object is of type B the method in class B is invoked.

作者: neojones 发布者: 2017 年 12 月 27 日

0

638 作者的声誉

You have created Object of Child class and an Object of type Reference. The reference is of type Parent (Down-casting)

When you call any method on reference, the method of type reference will be invoked. But in case the method is not available in reference type, it will be called from Parent (Inheritance).

In your example compiler checks for a method1 in object A and finds it there. Second when method2 is called its invoked from the current Object (Object B instead of from Reference A).

Note since the method2 is overriden when you call it from reference of A it will invoke method from Object A (Parent) and if you invoke it from Reference B it will invoke from Object B(Child).

Key point to remember here is the difference in Reference and Object. They both are different entities.

*The same thing will happen if you call a concrete method from abstract class which calls an abstract method of same class. The child class's implementation method will get called.

作者: amitmah 发布者: 2017 年 12 月 27 日
32x32