Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why inner class can override private final method?

I wondered if it makes sense to declare a private method as final as well, and I thought it doesn't make sense. But I imagined there's an exclusive situation and wrote the code to figure it out:

public class Boom {      private void touchMe() {         System.out.println("super::I am not overridable!");     }      private class Inner extends Boom {          private void touchMe() {             super.touchMe();             System.out.println("sub::You suck! I overrided you!");         }     }      public static void main(String... args) {         Boom boom = new Boom();         Boom.Inner inner = boom.new Inner();         inner.touchMe();     } } 

It compiled and worked. "I should make touchMe() final" I thought and did it:

public class Boom {      private final void touchMe() {         System.out.println("super::I am not overridable!");     }      private class Inner extends Boom {          private void touchMe() {             super.touchMe();             System.out.println("sub::You suck! I overrided you!");         }     }      public static void main(String... args) {         Boom boom = new Boom();         Boom.Inner inner = boom.new Inner();         inner.touchMe();     } } 

and it also works and tells me

chicout@chicout-linlap:~$ java Boom super::I am not overridable! sub::You suck! I overrided you! 

why?

like image 650
chicout Avatar asked Mar 01 '12 21:03

chicout


People also ask

Can we override private method in inner class?

1) In Java, inner Class is allowed to access private data members of outer class. This behavior is same as C++ (See this). 2) In Java, methods declared as private can never be overridden, they are in-fact bounded during compile time.

Why private methods Cannot be overridden?

You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it's not accessible there.

Can we override private and final methods?

No, we cannot override private or static methods in Java. Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.

Why Private methods are available in subclasses if they are not accessible in subclasses?

Private methods are inherited in sub class ,which means private methods are available in child class but they are not accessible from child class,because here we have to remember the concept of availability and accessibility.


1 Answers

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?
like image 120
aioobe Avatar answered Oct 23 '22 05:10

aioobe