- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Private and final methods in Java Programming
In Java private methods are the methods having private access modifier and are restricted to be access in the defining class only and are not visible in their child class due to which are not eligible for overridden. However, we can define a method with the same name in the child class and could access in parent class.
Like private methods final methods in Java are the methods having final non-access modifier instead of private and are again restricted to be accessed in the defining class only and are not visible in their child class due to which are not eligible for overridden. The only difference between private and final methods is that in case of final methods we even can't define a method with the same name in child class while in case of private methods we could define.
In Java as both private and final methods do not allow the overridden functionality so no use of using both modifiers together with same method.
Example
public class PrivateFinalMethods { private void print() { System.out.println("in parent print"); } public static void main(String[] args) { PrivateFinalMethods obj = new PrivateFinalMethodsChild(); obj.print(); PrivateFinalMethodsChild obj1 = new PrivateFinalMethodsChild(); obj1.print(); } } class PrivateFinalMethodsChild extends PrivateFinalMethods { public void print(){ System.out.println("in child print method"); } }
Output
in parent print in child print method