Sharuk Ahmed
Sharuk Ahmed

Reputation: 855

Java method access on runtime polymorphism

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

Upvotes: 2

Views: 157

Answers (5)

Amit Mahajan
Amit Mahajan

Reputation: 915

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.

Upvotes: 0

neojones
neojones

Reputation: 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.

Upvotes: 0

Muhammad Soliman
Muhammad Soliman

Reputation: 23756

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();

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70909

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.

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

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

Upvotes: 0

Related Questions