This, Final Keywords
This, Final Keywords
Module-2
Can access any instance variable and method by using this keyword.
The main purpose is to solve the confusion when we have same variable name for instance and
local variables.
By using ‘this’ keyword, we can call the attributes, methods and constructors of the current class.
Dr.M.Sivakumar SIMATS 2
‘this’ keyword
Calling instance variable
Dr.M.Sivakumar SIMATS 3
‘this’ keyword
Calling a constructor Calling a method
class Demo class Demo
{ {
Demo () public void getName()
{ {
// Calling constructor System.out.println(“I am a method”);
this(“I am a constructor”); }
} public void display()
Demo(String str){ {
System.out.println(str); this.getName(); // Calling a method
} }
public static void main(String[] args) { public static void main(String[] args) {
Demo d = new Demo(); Demo d = new Demo();
} d.display();
} }
}
Dr.M.Sivakumar SIMATS 4
‘this’ keyword
Note:
The main purpose of using "this" keyword in real life application is to differentiate variable of
class or formal parameters of methods or constructor
Dr.M.Sivakumar SIMATS 5
‘super’ keyword
‘super’ Keyword is a reference variable that is used in inheritance to refer to the immediate
parent class object from the child class.
Uses
To access the parent class variable
Dr.M.Sivakumar SIMATS 6
‘super’ keyword
Calling Variable Calling method
class Employee { class Employee {
String role = "Software Engineer"; String role = "Software Engineer";
} public void displayRole() {
public class Developer extends Employee { System.out.println("Employee Role: " + role);
String role = "Developer"; }
public void displayRole() { }
System.out.println(role); public class Developer extends Employee {
System.out.println(super.role); String role = "Developer";
} public void displayRole() {
public static void main(String[] args) { System.out.println(role);
Developer d = new Developer(); super.displayRole();
d.displayRole(); } }
} public static void main(String[] args) {
Developer d = new Developer();
d.displayRole();
}
}
Dr.M.Sivakumar SIMATS 7
‘super’ keyword
Calling constructor
Dr.M.Sivakumar SIMATS 8
‘super’ keyword
Calling Parameterized constructor
class Shapes {
Shapes() {
System.out.println("This is a shape");
}
Shapes(String name) {
System.out.println("Shape name is: " + name);
}
}
public class Square extends Shapes {
Square() {
super("Square"); // calls base class parameterized constructor
System.out.println("Square constructor");
}
public static void main(String[] args) {
Square s = new Square();
}
}
Dr.M.Sivakumar SIMATS 9
‘final’ keyword
It is used to make a variable as a constant, restrict method overriding and restrict inheritance.
Dr.M.Sivakumar SIMATS 10
‘final’ variable
Final keyword is used to make a variable as a constant.
A variable declared with the final keyword cannot be modified by the program after initialization
Dr.M.Sivakumar SIMATS 11
‘final’ method
It makes a method final, meaning that sub classes can not override this method.
The compiler checks and gives an error if you try to override the method.
Dr.M.Sivakumar SIMATS 12
‘final’ class
It makes a class final, meaning that the class can not be inheriting by other classes.
Dr.M.Sivakumar SIMATS 13