Access Modifier in Java is the reserved keyword used to define the scope of a class, variable, and methods. It also tells us about that whether child class creation is possible or not or whether object creation is possible or not.
Abstract Access Modifier is a modifier applicable only for classes and methods but not for variables. If we declare any method as abstract then that method must have its implementation in the child class of the respective class because abstract methods never talk about implementation. If any modifier talks about implementation then it forms an illegal combination with an abstract modifier. In a similar way if for any java class if we are not allowed to create an object (because of partial implementation) then such type of class we have to declare with abstract modifier.
Example:
Java
// Java program to illustrate Abstract Access Modifier // Importing the required packages import java.io.*; import java.util.*; // Class 1 // Helper abstract class abstract class Vehicle { // Declaring an abstract method getNumberOfWheel abstract public int getNumberOfWheel(); } // Class 2 // Helper class extending above abstract class class Bus extends Vehicle { // Giving the implementation // to the parent abstract method public int getNumberOfWheel() { return 7 ; } } // Class 3 // Helper class extending above abstract class class Auto extends Vehicle { // Giving the implementation // to the parent abstract method public int getNumberOfWheel() { return 3 ; } } // Class 4 // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating Bus object Bus b = new Bus(); // Creating Auto object Auto a = new Auto(); // Now getting and displaying // the number of wheels // for Bus by calling the // getNumberOfWheel method System.out.println( "Number of wheels in bus is" + " " + b.getNumberOfWheel()); // Now getting and displaying // the number of wheels // for Auto by calling the // getNumberOfWheel method System.out.println( "Number of wheels in Auto is" + " " + a.getNumberOfWheel()); } } |
Number of wheels in bus is 7 Number of wheels in Auto is 3
Now dwelling onto the next modifier is the public Access Modifier. So If a class is declared as public then we can access that class from anywhere. We will be creating a package pack1 inside that package we declare a class A which is public and inside that class, we declare a method m1 which is also public. Now we create another package pack2 and inside that pack2 we import pack1 and declare a class B and in class B’s main method we create an object of type class A and trying to access the data of method m1.
Example 1:
Java
// Java Program to illustrate public modifier // Creating a package pack1 package pack1; // Declaring a class A public class A { // Declaring a public method m1 public void m1() { // Print statement System.out.println( "GFG" ); } } |
Output:
Example 2:
Java
// Java Program to Illustrate Public Modifier // Creating package pack2 package pack2; // Importing package pack1; import java.io.*; // Importing the required modules import java.util.*; import pack1.*; // Main class // Declaring a class B class B { // Main driver method public static void main(String[] args) { // Creating object of type class A A b = new A(); // Now calling the method m1() A.m1(); } } |
Output:
Finally after having an adequate understanding of both of them let us conclude out the differences between them to click better understanding as depicted in the below table as follows:
Abstract Access Modifier | Public Access Modifier |
---|---|
This modifier is not applicable for variables. | This modifier is applicable for variables. |
This modifier is not applicable for enum. | This modifier is applicable for both outer and inner enum. |
This modifier is not applicable for constructors. | This modifier is applicable for constructors. |
This modifier is more restricted than the public access modifier. | This modifier is less restricted than the abstract access modifier. |