Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected Behavior wrt the final modifier

This is my code

package alpha ;

class A1
{
    static class A11
    {
        private
            final // WHAT IS THE EFFECT OF THIS MODIFIER?
            void fun ( String caller )
            {
                System . out . println ( "A11:\t" + caller ) ;
            }
    }

    static class A12 extends A11
    {
        private void fun ( String caller )
        {
            super . fun ( caller + caller ) ;
        }
    }

    public static void main ( String [ ] args )
    {
        A12 a12 = new A12 ( ) ;
        a12 . fun ( "Hello" ) ;
    }
}

I have found that with or without the final mdifer in A1.A11 the program compiles and runs.

I can understand that without the final modifier, A1.A12 can see and thus override the fun method. It is private but they are in the same class so there is no visibility issue.

I can not understand why it works with the final modifier. Should not the overriding in A1.A12 be prohibited?

This is the output of the program with the final modifer in place

java alpha/A1
A11:    HelloHello

If it was simply ignoring the other fun method then

  1. would not the compiler have complained about the super reference
  2. the A11 would not be in the output
like image 594
emory Avatar asked Aug 20 '26 19:08

emory


1 Answers

Your methods are private.

Change their visibility to protected to see expected behavior, that is, only when the method is protected, public or default visibility, the concept of overriding even exists.

Doing something like this --

class A1
{
    static class A11
    {
        public
            final // WHAT IS THE EFFECT OF THIS MODIFIER?
            void fun ( String caller )
            {
                System . out . println ( "A11:\t" + caller ) ;
            }
    }

    static class A12 extends A11
    {
        public void fun ( String caller )
        {
            super . fun ( caller + caller ) ;
        }
    }

    public static void main ( String [ ] args )
    {
        A12 a12 = new A12 ( ) ;
        a12 . fun ( "Hello" ) ;
    }
}

will now throw the compile-time exception

fun(java.lang.String) in A1.A12 cannot override fun(java.lang.String) in A1.A11; overridden method is final
like image 152
Kal Avatar answered Aug 23 '26 08:08

Kal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!