Nilesh
Nilesh

Reputation: 281

Java dynamic binding calling method of parent class

Below is the code which I am trying to analyze.

class Acc{
    public void aMethod(List<Integer> A){
        System.out.println(A.toString());
    }
}

class Back extends Acc{
    public void aMethod(String A){
        System.out.println(A);
    }
}

Here if I invoke it as

Acc a = new Back();
a.aMethod(list);

But upon debugging the method of parent class is being called.But it should dynamically call method of Back as per my understanding.Please help me understand of this behaviour.Now again If I keep parent class argument as List<Integer> and child one as List<String> then it gives a name clash thing.Why not in this case?

Upvotes: 0

Views: 1122

Answers (2)

Abhijit Bashetti
Abhijit Bashetti

Reputation: 8678

Here are some rules for method overriding:

The argument list should be exactly the same as that of the overridden method.

The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.

The access level cannot be more restrictive than the overridden method's access level. For example: if the superclass method is declared public then the overridding method in the sub class cannot be either private or protected.

Instance methods can be overridden only if they are inherited by the subclass.

A method declared final cannot be overridden.

A method declared static cannot be overridden but can be re-declared.

If a method cannot be inherited, then it cannot be overridden.

A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.

A subclass in a different package can only override the non-final methods declared public or protected.

An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.

If you check this and then code written, it would be clear where its wrong.

Upvotes: 0

gprathour
gprathour

Reputation: 15333

Parent class method public void aMethod(List<Integer> A) and child class method is public void aMethod(String A).

Both have different parameters, so it is not Method Overriding. However in child class it can be thought of as Method Overloading.

When you call the method, you are passing the list parameter and there is only only method that matches this signature and that is of parent class. So the method of parent class gets called.

Method Overriding

For method overriding methods in base class and child class must have same signature i.e. same name and same arguments. However method in child class can have higher scope access specifier than base class's method.

In simpler words, if method in base class is default then method in child class can be public.

Upvotes: 4

Related Questions