Access modifiers in java

16
Access Modifiers in Java Sourabrata Mukherjee

Transcript of Access modifiers in java

Page 1: Access modifiers in java

Access Modifiers in JavaSourabrata Mukherjee

Page 2: Access modifiers in java

Topics:●Why Access Modifiers?

●Private

●Default (package)

●Protected

●Public

●Access Modifiers and Inheritance

●Access Modifiers on Local Variables

●Conclusion

Page 3: Access modifiers in java

Why Access Modifiers?The use of modifiers goes to the core concepts of encapsulation, aka 'data hiding' in object-oriented development. Variables should never be public. That is the whole point of private/protected modifiers on them: to prevent direct access to the variables themselves. You provide methods to manipulate variables. A method has to be public in order to allow other programmers (or classes) access to that data. But by using methods, you can control how a variable is manipulated. Which is the entire point because you've hidden the details of the variable behind the method.

Page 4: Access modifiers in java

Continue..Let Suppose you have class which contains two variable age and name. Now you have created getter setter for these variable. It is obvious that variable which we declared we will mark it as private, Why because i don't want to give the accessibility of that variable to other class directly. Now The question is why i don't want to give accessibility directly to other class?. The Answer is because i want to control my variable. If i will give direct accessibility to other class, Other class can assign age as negative which is not good, age cannot be a negative value. Hence to control this thing i am exposing getter and setter method.

Page 5: Access modifiers in java

Continue..Other class object will call my setter method and supply value. Inside method i can control the value like if age is negative simply assign 0 or if age is more than 200 throw exception saying age cannot be 200. Now what is another class object want to read data from variable. For that i have given one getter method which would be public in nature. Since my variable is private hence other class object cannot directly access my variable hence this getter method is required which will simply return the variable. Access modifier are used to control the visibility of any variable or method and this is necessary .

Page 6: Access modifiers in java

PrivateThe private access modifier is accessible only within class.

A class cannot be private or protected except nested class.

If you make any class constructor private, you cannot create the instance of that class from outside the class.

Page 7: Access modifiers in java

Continue..private Constructors:

If a constructor in a class is assigned the private Java access modifier, that means that the constructor cannot be called from anywhere outside the class. A private constructor can still get called from other constructors, or from static methods in the same class. Here is an example,

Page 8: Access modifiers in java

Continue..public class Clock {

private long time = 0;

private Clock(long time) {

this.time = time;

}

public Clock(long time, long timeOffset) {

this(time);

this.time += timeOffset;

}

public static Clock newClock() {

return new Clock(System.currentTimeMillis());

}

}

Page 9: Access modifiers in java

Continue..This version of the Clock class contains a private constructor and a public constructor. The private constructor is called from the public constructor (the statement this();). The private constructor is also called from the static method newClock(). The above example only serves to show you that a private constructor can be called from public constructors and from static methods inside the same class. Do not perceive the above example as an example of clever design in any way.

Page 10: Access modifiers in java

DefaultIf you don't use any modifier, it is treated as default by default. The default modifier is accessible only within package.

The default Java access modifier is declared by not writing any access modifier at all. The default access modifier means that code inside the class itself as well as code inside classes in the same package as this class, can access the class, field, constructor or method which the default access modifier is assigned to. Therefore, the default access modifier is also sometimes referred to as the package access modifier.

Page 11: Access modifiers in java

ProtectedThe 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.

The protected access modifier provides the same access as the default access modifier, with the addition that subclasses can access protected methods and member variables (fields) of the superclass. This is true even if the subclass is not located in the same package as the superclass.

Page 12: Access modifiers in java

PublicThe public access modifier is accessible everywhere. It has the widest scope among all other modifiers.

The accessing code can be in a different class and different package.

Page 13: Access modifiers in java

Access Modifiers and InheritanceWhen you create a subclass of some class, the methods in the subclass cannot have less accessible access modifiers assigned to them than they had in the superclass. For instance, if a method in the superclass is public then it must be public in the subclass too, in case the subclass overrides the method. If a method in the superclass is protected then it must be either protected or public in the subclass. While it is not allowed to decrease accessibility of an overridden method, it is allowed to expand accessibility of an overridden method. For instance, if a method is assigned the default access modifier in the superclass, then it is allowed to assign the overridden method in the subclass the public access modifier.

Page 14: Access modifiers in java

Access Modifiers on Local VariablesNo Access Modifiers can be applied to local variables. Only final can be applied to a local variable which is a Non Access Modifier .

Page 15: Access modifiers in java

Conclusion

Page 16: Access modifiers in java

Thank You