I have a very early question about polymorphism / access modifiers in Java.
I would like to find out why a certain phenomenon occurs in my code. Here 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, when you change the modifier from metodo1()
from class Pai
to private
, the return of main
method becomes " Metodo 01 Pai
". I would like to understand why this change occurs.