The solution was : we change the access modifier protected of the parent class method initialize() to private . This is confusing , because the method wasn't even executed when it's protected . I checked that using : System.out.println("This line is an output from the parent class method initialize() :");
and the Output was :
0
9
Process finished with exit code 0
A protected method is supposed to be seen and used from outside the class in the same package and in all child classes . There probably some detail I am misunderstanding here!
package de.codegym.task.task15.task1526;
/*
Debuggen, debuggen und nochmals debuggen
*/
public class Solution {
public static void main(String[] args) {
new B(6);
}
public static class A {
protected int f1 = 7;//Nothing
// Nothing changes if I change the initialised value because it will be anyways changed by the constructor
public A(int f1) {
this.f1 = f1;
initialize();
}
protected void initialize() {
System.out.println("This line is an output from the parent class method initialize() :");
System.out.println(f1);
}
}
public static class B extends A {
protected int f1 = 3;
public B(int f1) {
super(f1);//is used to refer immediate parent class!
this.f1 += f1;
initialize();
}
protected void initialize() {
System.out.println(f1);
}
}
}