I don't know why you made all methods private. If you didn't, your question would actually make perfect sense. Consider this code:
class Super{
void method() {}
}
class Sub extends Super {
void method(int x) {}
}
Now, even though it just declares one, the class Sub
actually has two methods named method
, therefore that method is overloaded for Sub
. The class Super
is unaffected because it still has just one method
.
As an aside, the most notorious example where the above gets in the way of program correctness involves the standard method equals
. Beginners are tempted to implement it just for the specific type:
public class Thing {
public boolean equals(Thing that) { ...compare by object contents... }
}
but this doesn't override Object.equals
, so now the class has two equals
methods. The worst thing comes when some code accidentally uses the specific overload, whereas other code uses the general one:
Thing t1 = new Thing(), t2 = new Thing();
System.out.println(t1.equals(t2)); // true, great
Object o1 = t1, o2 = t2;
System.out.println(o1.equals(o2)); // now suddenly false
System.out.println(t1.equals(o2)); // false again
System.out.println(o1.equals(t2)); // still false