0

The below program prints ParentClass a() -

public class StaticBindingTest {

    public static void main(String[] args) {
        ParentClass p = new ChildClass();

        p.a("abc");
    }       
}

class ParentClass{

    public void a(Object o){
        System.out.println("ParentClass a()");  
    }
}

class ChildClass extends ParentClass{

    public void a(String s){
        System.out.println("ChildClass a()");   
    }
}

However, if I change ParentClass a() to accept String, the program prints Childclass a(). Why? -

package com.my.test;

public class StaticBindingTest {

    public static void main(String[] args) {
        ParentClass p = new ChildClass();

        p.a("abc");
    }       
}

class ParentClass{

    public void a(String o){
        System.out.println("ParentClass a()");  
    }
}
class ChildClass extends ParentClass{

    public void a(String s){
        System.out.println("ChildClass a()");   
    }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
Puneet
  • 67
  • 3

2 Answers2

0

Which overload of a method to call is decided at compile time. Which implementation of a method to call is decided at runtime.

In the first situation, ChildClass has essentially two overloads of a. One that accepts a String and one that accepts Object.

Your variable p's compile time type is ParentClass. The compiler does not actually know it is actually a ChildClass. That has to wait till runtime. So the compiler looks into ParentClass and see that there is only one overload of a in ParentClass so it decides to call that overload, hence the output.

In the second situation, there is 1 overload, but 2 implementations for a in ChildClass.

The compiler sees your p as ParentClass as usual and this time, it chooses the overload that accepts a String, which is the only one to choose here. Now at runtime, a is called. Since p is actually a ChildClass instance, it calls the implementation in ChildClass, hence the output.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

The reason is that the child class then Override the parent's implementation. There is a brief overview of how things are processed when you define a method with the same signature as a method in a superclass :

enter image description here

In a subclass, you can overload the methods (as in your first example) inherited from the superclass. Such overloaded methods neither hide nor override the superclass instance methods—they are new methods, unique to the subclass.


When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error.

Naman
  • 27,789
  • 26
  • 218
  • 353