In the below code, the child class object calls its getBankName() method but instead, the private method getBankName() of parent class is invoked.
public class Bank {
private void getBankName() {
System.out.println("Bank");
}
public static void main(String[] args) {
Bank bank = new MyBank();
bank.getBankName();
}
}
class MyBank extends Bank {
public void getBankName() {
System.out.println("MyBank");
}
}
Further, if I change the access specifier of parent's method to public, then it works fine(child object calls its own method and prints 'MyBank'). Why is the invocation getting affected just because of the access specifier of the parent method??