Private, Public, Protected and Default in Java

Tram Ho

1. Private, Public, Protected and Default?

Private, public, protected and default are referred to collectively as Access modifiers, or access range keywords.

So, what is Access Modifier ?

Access modifiers are “words” that precede the declarations of a class, variable or method (method) to express the accessibility of that class, variable or method in other classes. With class we have 2 types of Access modifier: public and default, but with variables and methods (methods), we have 4 access modifiers (public, protected, default, private).

Default: Internal package access

Private: Internal access for grades

Public: Public component, freely accessible from the outside

Protected: Protected components, restricted from external access

2. Public Access Modifier

Public Access Modifier is accessible anywhere. It has the widest range of all modifiers. A public declared class, method, constructor, interface, … can be accessed from any other class. Therefore, fields, methods, and blocks declared inside a public class can be accessed from any class in the Java world.

However, if the public class we are trying to access is in another package, this public class still needs to be imported before accessing.

3. Private Access Modifier

Methods, variables and constructors that are declared private can only be accessed within the declared class itself.

Private Modifier has the most restrictive access range. Classes and interfaces cannot be private.

Private declared variables can be accessed outside the class if a public getter is created for that variable at that class.

Using Private Access Modifier in Java is a key way for an object to encapsulate itself and hide data from outside, helping to secure data from outside.

4. Protected Access Modifier

The Protected Access Modifier is accessible inside the package and outside the package but only through inheritance.

The Protected Access Modifier cannot be applied to classes and interfaces. Methods and fields can be protected, but methods and fields in an interface cannot be protected.

5. Default Access Modifier

Default Access Modifier means that we do not explicitly declare an Access Modifier for a class, field, method, etc. In other words, if you do not use any Modifier, it is viewed by default. as default. Default Modifier is only accessible within the package.

A variable or method declared without any Access Modifier is available to all classes in the same package. These fields in an interface are completely public static final and methods in an interface are public by default.

6. Access Modifier and inheritance in Java

Rules to note and mandatory:

Methods declared public in a parent class must also be public in all subclasses.

Methods declared in a parent class must either be protected or public in subclasses; they cannot be private.

Methods declared without access control (without any modifiers) can be declared private in subclasses.

Private declared methods are not inherited, so there is no rule for them.

Share the news now

Source : Viblo