0% found this document useful (0 votes)
6 views13 pages

This, Final Keywords

java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

This, Final Keywords

java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

This, super and Final

Module-2

Professor & Technical Trainer / CSE


Saveetha School of Engineering, SIMATS
[email protected]
+91-9500600868
‘this’ keyword
 ‘this’ is a reference variable that refers to the current object.

 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 scope of "this" keyword is within the class.

 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

 To invoke the parent class method

 To invoke the parent class constructors with and without argument.

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.

 In java language final keyword can be used in following way

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.

 When we want to restrict inheritance then make class as a final.

Dr.M.Sivakumar SIMATS 13

You might also like