Search code examples
javaprivatefinal

Can we take private methods and final methods are same


As far as I understand, private methods and final methods are the same. The Java compiler determines the meanings of private methods at compile time. Neither private methods nor final methods can be modified at runtime, and neither can be overridden. These similarities make it seem like private and final methods are the same. Is this true?


Solution

  • A private method is automatically final and hidden from its derived class. A final class is not hidden from its derived class. Therefore you can make a new class with the same name as the private method such as

    class test {
        private void works {
        }
    }
    class tester extends test {
        private void works {
        }
    }
    

    but you cannot make a new class with the same name as the final method

    /*----------Doesn't Work------------*/
    class test {
        final void dWorks {
        }
    }
    class tester extends test {
        final void dWorks {
        }
    }
    

    Example and answer here : http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ309_006.htm