Skip to content
Related Articles

Related Articles

Improve Article
Private vs Final Access Modifier in Java
  • Last Updated : 16 Jun, 2021

Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by using an appropriate keyword in java called access modifiers. So access modifiers are used to set the accessibility of classes, methods, and other members.

Private Access Modifiers:

This modifier is not applicable for top-level classes or interfaces. It is only applicable to constructors, methods, and fields inside the classes. If a variable or methods or constructor is declared as private then we can access them only from within the class i.e from outside the class we can’t access them.

Java




// Java Program to illustrate Private Access Modifier
   
// import required packages
import java.io.*;
   
import java.util.*;
   
// helper class
class A {
     
    // helper method
    private void m1() { System.out.println("GFG"); }
}
   
// driver class
class B {
     
    // main method
    public static void main(String[] args)
    {
        // creating an object of type class A
        A a = new A();
         
        // accessing the method m1()
        a.m1();
    }
}


 



Final Access Modifier:

It is a modifier applicable to classes, methods, and variables. If we declare a parent class method as final then we can’t override that method in the child class because its implementation is final and if a class is declared as final we can’t extend the functionality of that class i.e we can’t create a child class for that class i.e inheritance is not possible for final classes. Every method present inside the final class is always final y default but every variable present inside the final class need not be final. The main advantage of the final keyword is we can achieve security and we can provide a unique implementation. But the main disadvantage of the final keyword is we are missing key benefits of OOPs like Inheritance(Because of the final class), Polymorphism(Because of the final method)  hence if there are no specific requirements then it is not recommended to use the final keyword. 

Java




// Java program to illustrate Final keyword
 
// import required packages
import java.io.*;
import java.util.*;
// Declaring parent class P
class P {
    // Declaring a first name
    // method
    public void firstName()
    {
        // Display firstname
        System.out.println("Rahul ");
    }
    /// Declaring a final surName
    // method
    public final void surName()
    {
        // Display surname
        System.out.println("Trivedi");
    }
}
// Creating a child class
// of above parent class
class C extends P {
    // overriding the surName
    // method
    public void surName()
    {
        // Display surname
        System.out.println("Sharma");
    }
    // Main method
    public static void main(String[] args)
    {
        // Display message
        System.out.println("GFG");
    }
}

 

                     Private Access Modifier                         Final Access Modifier 
This modifier is not applicable to top-level classes.This modifier is applicable to top-level classes. 
We cannot access private methods outside the class.We can access the final method outside the class.
We can hide the private method.The final method cannot be hidden.
This modifier is applicable to both the enum and constructor.The final modifier is not applicable to both the enum and constructor. 
This modifier is not applicable for local variables.The final modifier is the only modifier which is applicable for local variables.

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :