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?
Parent class method
public void aMethod(List<Integer> A)
and child class method ispublic void aMethod(String A)
.Both have different parameters, so it is not
Method Overriding
. However in child class it can be thought of asMethod 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 bepublic
.