There are three types of access modifiers in Java. public, private, protected. If no access modifier is specified then it has a default access.
Object-oriented programming is used to set access modifiers, which control how accessible Java classes, constructors, methods, and other members are. We can control the scope or accessibility of these classes, methods, constructors, and other components using the access modifiers.
Access Modifiers in Java code example
- class A{
- protected void msg(){System.out.println(“Hello java”);}
- }
- public class Simple extends A{
- void msg(){System.out.println(“Hello java”);}//C.T.Error.
- public static void main(String args[]){
- Simple obj=new Simple();
- obj.msg();