Overriding Private Methods in Java

Override private method in java

You cannot override a private method. It isn't visible if you cast A to B. You can override a protected method, but that isn't what you're doing here (and yes, here if you move your main to A then you would get the other method. I would recommend the @Override annotation when you intend to override,

class A extends B {
@Override
public void don() { // <-- will not compile if don is private in B.
System.out.println("hoho public");
}
}

In this case why didn't compiler provide an error for using t.don() which is private?

The Java Tutorials: Predefined Annotation Types says (in part)

While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

How to override private method

It is not possible to override private methods, neither in Kotlin nor in Java.

Reflection does not help here. Private methods are dispatched statically, not dynamically -- that is, the correct implementation is chosen at compile time, not runtime.

See also:

  • Override "private" method in java
  • Overriding private methods in Java

To "achieve what you want", you should go another route -- if the API you use foresees extension, it will likely offer that in one way or another.

Overriding private methods

3: Teacher can override existing private methods of Person.

That's wrong.

Private methods/fields of super class are not visible/inherited to sub classes. So, You can't override them.

Try it with an example

Why are private methods in Java implicitly final?

Final methods cannot be overriden, but they can be called from a subclass.

This may or may not be true, depending on the modifier, and where your overriding class is. If the method is private or default (no modifier) then you cannot call the method from a subclass, unless your subclass is in the same package.

Private methods cannot be overriden, and they cannot be called from a subclass.

True. The reason private methods cannot be overridden, is because they can't be seen. This does not imply that methods that cannot be overridden are private however.

Override a private method in Kotlin

It is not possible to override private methods, neither in Kotlin nor in Java.

Why inner class can override private final method?

Private methods can not be overridden (private methods are not inherited!) In fact, it makes no difference if you declare a private method final or not.

The two methods you have declared, Boom.touchMe and Boom.Inner.touchMe are two completely separate methods which just happen to share the same identifier. The fact that super.touchMe refers to a different method than touchMe, is just because Boom.Inner.touchMe shadows Boom.touchMe (and not because it overrides it).

This can be demonstrated in a number of ways:

  • As you discovered yourself, if you change the methods to be public, the compiler will complain because you are suddenly trying to override a final method.

  • If you keep the methods private and add the @Override annotation, the compiler will complain.

  • As alpian points out, if you cast the Boom.Inner object to a Boom object (((Boom) inner).touchMe()) the Boom.touchMe is called (if it indeed was overridden, the cast wouldn't matter).

Related question:

  • Make private methods final?


Related Topics



Leave a reply



Submit