Encapsulation :
To define what level of information share with other word in Java is called Encapsulation. Access restriction level is achieved by Access modifiers (Privet, Public, Package, Protected), These are 4 Ps of Object oriented program .Encapsulation in java is a process of wrapping code and data together into a single unit.Access Modifiers :
Four access modifier available in Java as Package Privet, Protected, Public.Package access modifier is default access modifier, that means if we don't use any access modifier then Package level restriction will be applied.
Public Access modifier is the least restrictive where as Privet is most restrictive.
There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc. Here, we will learn access modifiers.
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
Public | Y | Y | Y | Y |
Protected | Y | Y | Y | N |
Package | Y | Y | N | N |
Private | N | Y | N | N |
1. Public Access Modifier:
Public access modifier is least restrictive and class ,method ,interface and variables with this access modifier are accessible from any class in Java.However if the public class we are trying to access is in a different package, then the public class still need to be imported.
Because of class inheritance, all public methods and variables of a class are inherited by its sub classes.
2. Package Access Modifier (default):
If we don't use any access modifiers than Default access modifier Package is considered by default. In this case class ,method and variable will be access withing same package. we can not use class, method and variable defined as package or no access modifiers outside package.3. Protected Access Modifier :
The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.
4. Privet Access Modifier :
Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
Variables that are declared private can be accessed outside the class if public getter methods are present in the class.
Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world.
No comments:
Post a Comment