Video in progress

Strong Junior 🧱 OOP and SOLID

If you write a `private` method of the parent class again in the child class, is that an override?

No. A private method is not visible to the child class, so a method with the same name in the child is a completely new, independent method. When called from inside the parent class, the parent's own private version always runs.

📖 Answer

class Base {
    private String name() { return "base"; }
    void print() { System.out.println(name()); }
}
class Child extends Base {
    String name() { return "child"; }   // not an override, a new method
}

new Child().print();   // "base"

The name() call inside print() is bound at compile time to Base's private method. There is no dynamic dispatch for private methods. If you put @Override on name() in the child class, the compiler gives an error right away, which is why it is useful to always write the annotation.

This is sometimes used on purpose: the parent class protects its internal helper method from being "broken" by the children. If the children need to change the behavior, make the method protected and document it.

A similar case: a package-private method is not overridden in a child class in another package either.