I couldn't find a question like mine, so I hope it's not a duplicate one.
Again it's about overriding and hiding. I think - but I might be wrong - I understood both.
The following code behaves as expected, both methods have been hidden. method1 because it is a private method and private methods can't be overridden only hidden, method2 because it's static and static methods can't be overridden, they can only be hidden.
public class Child extends Parent {
public void method1(){System.out.println("child");}
public static void method2(){ System.out.println("static child");}
}
class Parent{
private void method1(){ System.out.println("parent");}
public static void method2(){ System.out.println("static parent");}
public static void main(String[] args){
Parent p = new Child();
p.method1(); //prints out "parent"
p.method2(); //prints out "static parent"
}
}
If I read the specs it says:
http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.3.3
A method can be declared final to prevent subclasses from overriding or hiding it.
If I change method1 in the Parent class to "final"
private final void method1(){ System.out.println("parent");}
Everything works fine. edit start: I expected an compiler error saying that final methods can't be hidden, but that didn't happen. :edit end
Question no 1: does that mean only static methods can be hidden? In the book I'm reading (OCA study guide, Jeanne Boyarsky and Scott Selikoff page 252) they clearly say that a private method was hidden.
Then I changed method2 in the Parent class to
public final static void method2(){ System.out.println("static parent");}
Now the compiler does complain, the error says "Child cannot override method2()" which is pretty confusing because I thought I tried to hide a method.
Question no 2: Shouldn't it be "Child cannot hide method2()"?
edit start: I am well aware that no overriding happens here, but as the mentioned specs point out: the modifier final prevents methods to be overridden or hidden, that's why I put it in the title. :edit end
static
method, only hide it: docs.oracle.com/javase/tutorial/java/IandI/override.html – Doubledecker