Abstract vs Public Access Modifier in Java
Last Updated :
03 Mar, 2022
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
import java.io.*;
import java.util.*;
abstract class Vehicle {
abstract public int getNumberOfWheel();
}
class Bus extends Vehicle {
public int getNumberOfWheel() { return 7 ; }
}
class Auto extends Vehicle {
public int getNumberOfWheel() { return 3 ; }
}
public class GFG {
public static void main(String[] args)
{
Bus b = new Bus();
Auto a = new Auto();
System.out.println( "Number of wheels in bus is"
+ " " + b.getNumberOfWheel());
System.out.println( "Number of wheels in Auto is"
+ " " + a.getNumberOfWheel());
}
}
|
Output
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
package pack1;
public class A {
public void m1()
{
System.out.println( "GFG" );
}
}
|
Output:
Example 2:
Java
package pack2;
import java.io.*;
import java.util.*;
import pack1.*;
class B {
public static void main(String[] args)
{
A b = new A();
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. |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...