Access Modifiers Lect 4
Access Modifiers Lect 4
class Animal {
public void method1() {...}
In the above example, we have declared 2 methods: method1() and method2(). Here,
Note the keyword public and private. These are access modifiers in Java. They are also known as visibility
modifiers.
There are four access modifiers keywords in Java and they are:
Modifier Description
Default declarations are visible only within the package (package private)
If we do not explicitly specify any access modifier for classes, methods, variables, etc, then by default the
default access modifier is considered. For example,
package defaultPackage;
class Logger {
void message(){
System.out.println("This is a message");
}
}
Here, the Logger class has the default access modifier. And the class is visible to all the classes that
belong to the defaultPackage package. However, if we try to use the Logger class in another class outside
of defaultPackage, we will get a compilation error.
class Data {
// private variable
private String name;
}
The error is generated because we are trying to access the private variable of the Data class from the
Main class.
You might be wondering what if we need to access those private variables. In this case, we can use the
getters and setters method. For example,
class Data {
private String name;
// getter method
public String getName() {
return this.name;
}
// setter method
public void setName(String name) {
this.name= name;
}
}
public class Main {
public static void main(String[] main){
Data d = new Data();
Here, we have used the setter method (setName()) to assign value to the variable and the getter method
(getName()) to access the variable.
We have used this keyword inside the setName() to refer to the variable of the class. To learn more on
this keyword
class Animal {
// protected method
protected void display() {
System.out.println("I am an animal");
}
}
In the above example, we have a protected method named display() inside the Animal class. The Animal
class is inherited by the Dog class. To learn more about inheritance, visit Java Inheritance.
We then created an object dog of the Dog class. Using the object we tried to access the protected
method of the parent class.
Since protected methods can be accessed from the child classes, we are able to access the method of
Animal class from the Dog class
// Animal.java file
// public class
public class Animal {
// public variable
public int legCount;
// public method
public void display() {
System.out.println("I am an animal.");
System.out.println("I have " + legCount + " legs.");
}
}
// Main.java
public class Main {
public static void main( String[] args ) {
// accessing the public class
Animal animal = new Animal();
Here,
Access modifiers are mainly used for encapsulation. It can help us to control what part of a program can
access the members of a class. So that misuse of data can be prevented. To learn more about
encapsulation
this Keyword
this keyword is used to refer to the current object inside a method or a constructor. For
example,
class Main {
int instVar;
Main(int instVar){
this.instVar = instVar;
System.out.println("this reference = " + this);
}
Here, we can see that the reference of both obj and this is the same. It means this is nothing but the
reference to the current object.
In Java, it is not allowed to declare two or more variables having the same name inside a scope (class
scope or method scope). However, instance variables and parameters may have the same name. For
example,
class MyClass {
// instance variable
int age;
// parameter
MyClass(int age){
age = age;
}
}
In the above program, the instance variable and the parameter have the same name: age. Here, the Java
compiler is confused due to name ambiguity.
class Main {
int age;
Main(int age){
age = age;
}
In the above example, we have passed 8 as a value to the constructor. However, we are getting 0 as an
output. This is because the Java compiler gets confused because of the ambiguity in names between
instance the variable and the parameter.
class Main {
int age;
Main(int age){
this.age = age;
}
Also, if the name of the parameter and instance variable is different, the compiler automatically appends
this keyword. For example, the code:
class Main {
int age;
Main(int i) {
age = i;
}
}
is equivalent to:
class Main {
int age;
Main(int i) {
this.age = i;
}
}
class Main {
String name;
// setter method
void setName( String name ) {
this.name = name;
}
// getter method
String getName(){
return this.name;
}
While working with constructor overloading, we might have to invoke one constructor from another
constructor. In such a case, we cannot call the constructor explicitly. Instead, we have to use this
keyword.
Here, we use a different form of this keyword. That is, this(). Let's take an example,
class Complex {
private int a, b;
// constructor with 2 parameters
private Complex( int i, int j ){
this.a = i;
this.b = j;
}
@Override
public String toString(){
return this.a + " + " + this.b + "i";
}
// print objects
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
In the above example, we have used this keyword,
Here, when we print the object c1, the object is converted into a string. In this process, the toString() is
called. Since we override the toString() method inside our class, we get the output according to that
method.
One of the huge advantages of this() is to reduce the amount of duplicate code. However, we should be
always careful while using this().
This is because calling constructor from another constructor adds overhead and it is a slow process.
Another huge advantage of using this() is to reduce the amount of duplicate code.