Access modifiers in Java
Access modifiers in java are used to define the scope and visibility of classes, variables, methods, and constructors, they tell about which part of program can be accessed by other parts of the program according to there scope and visibility don't worry I will make you clear with the help of an example too but for now let's first see how many type of access modifiers are there in java.
Types of access modifiers in Java-
- Public - It can be accessed from anywhere in the code (class, package subclass or world)
- Private - It can be accessed from within the same class only.
- Protected - It can be accessed from the same package and from the subclasses of other packages too.
- Default - It can be accessed only within the same package.
Let's see each one of them with the help of an example too.
Example of public access modifier -
public class PublicExample {
public String name = "John";
public void display() {
System.out.println("Hello, " + name);
}
}
class Main {
public static void main(String[] args) {
PublicExample obj = new PublicExample();
obj.display();
}
}
Hello, John
Let me explain you what is happening in the above code name variable and display method are public so there is no problem to access these from Main class means no any issue accessing these public variable and method from other class hope that makes sense to you.
Example for private modifier -
class PrivateExample {
private int age = 25;
private void showAge() {
System.out.println("Age: " + age);
}
public void accessAge() {
showAge();
}
}
class Main {
public static void main(String[] args) {
PrivateExample obj = new PrivateExample();
obj.accessAge(); // Allowed
// obj.showAge(); // ❌ Error if uncommented: private access not allowed outside the class
}
}
Age: 25
In the above code snippet as you can see that showAge() method is private so it cannot be accessed from class Main from PrivateExample class object obj but can be easily accessed from accessAge() method because it is public hope it clears you the concept of private access modifier to you let's move forward.
class ProtectedExample {
protected String country = "USA";
protected void displayCountry() {
System.out.println("Country: " + country);
}
}
class Main extends ProtectedExample {
public static void main(String[] args) {
Main obj = new Main();
obj.displayCountry(); // Works because of inheritance
}
}
Country: USA
As you know protected member can be accessed within the same package but also from another package from subclass here in the above code snippet Main class extends from ProtectedExample class and displayCountry() method is protected so this method displayCountry() can easily be accessed here even if this class Main is in the other package also in that case too as I told you earlier already.
Example for Default -
class DefaultExample {
String city = "New York"; // default access
void displayCity() { // default access
System.out.println("City: " + city);
}
}
class Main {
public static void main(String[] args) {
DefaultExample obj = new DefaultExample();
obj.displayCity(); // Works only inside the same package
}
}
City: New York
In case of Default if you didn't specify any specifier java automatically specifies that as default and default means package private means it can be accessed from the same package only that's why here in the above example Main class is also in the same package as DefaultExample class that's why we can easily access everything what is there inside the DefaultExample class from our Main class hope it clears your concept completely now see you in some other article till then keep learning java.
Comments
Post a Comment
Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.