2
I have a very beginner question about polymorphism/access modifiers in Java.
I’d like to find out why a certain phenomenon occurs in my code. Below is an example of classes:
Class Pai
:
public class Pai {
public void metodo1() { //private dá problema
System.out.println("Metodo 01 Pai");
};
public void metodo2() {
metodo1();
}
}
Class that inherits the previous class:
public class Filha extends Pai{
public Filha() {
}
public void metodo1() {
System.out.println("Metodo 01 Filha");
}
}
And class Main
:
public class Main {
public static void main(String args[]) {
Pai instancia = new Filha();
instancia.metodo2();
}
}
The println
of Main
is "Metodo 01 Filha
". However, by changing the metodo1()
class Pai
for private
, the return of the method main
becomes "Metodo 01 Pai
". I would like to understand why this change occurs.