Abstract vs Public Access Modifier in Java
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.
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.
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.
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.