“this” and “extends” are the two reserved keywords in Java. “this” is a reference variable that refers to the current object inside a constructor or method of a class and specifies the state or behavior of the object whereas “extends” is used to drive one class from another class. Basically, we implement the concept of “inheritance” by using the “extends” keyword.
The outcomes of this write-up are:
- How to utilize the “this” Keyword in Java?
- How to utilize the “extend” Keyword in Java?
How to Use the “this” Keyword in Java?
“this” keyword is used to specify the current state or behavior of an object so that no confusion is left behind if we use parameters of the same name in a class. We can also invoke the constructor or method of the current class and pass arguments in the method or constructor call.
Check out the provided example to understand the working of the “this” keyword:
public class Student {
int reg_no;
String name;
public Student(int a, String b) {
this.reg_no = a;
this.name=b;
}
public String toString(){
return "Reg no= "+reg_no+" ,Name= "+name;
}
public static void main(String[] args) {
Student s1=new Student(25,"Alex");
Student s2= new Student(30,"Sarah");
System.out.println(s1);
System.out.println(s2);
}
}
Here:
- First of all, declare the “Student” class that has two parameters, such as “reg_no” to store and display the student’s registration number. The other one is “name” to show and save the student’s name.
- Then, invoke the constructor for the “Student” class that takes two parameters. Inside the constructor, we used the “this” keyword to store and display the value of the initialized variables.
- In the “main()” method, we created two objects “s1” and “s2” respectively, and stored the values for the registration number and name. Then, call the “System.out.println()” method to display the assigned values on the console.
Note: If we don’t use the “this” keyword, then the program will not show the values that we will initialize instead it will show 0 or null as output.
As you can see, the resultant values are shown in the below-stated output:
Let’s have a look at the below-given code block to check if we don’t use the “this” keyword then what will happen:
public class Student {
int reg_no;
String name;
public Student(int a, String b) {
}
public String toString(){
return "Reg no= "+reg_no+", Name= "+name;
}
public static void main(String[] args) {
Student s1=new Student(25, "Alex");
Student s2= new Student(30, "Sarah");
System.out.println(s1);
System.out.println(s2);
}
}
Here, we used the previously described example. As we discussed earlier, if the parameters are not initialized with the “this” keyword, then, the output will be 0 and null. As shown in the following output:
How to Utilize the “extends” Keyword in Java?
The “extends” keyword is utilized to derive(child) a class from the base(parent) class. In simple words, the “extends” keyword is used for inheritance(which is the feature of OOP) purposes. The new inherit class attains all the functionalities of the parent class.
Syntax
The general syntax of the “extends” keyword is as follows:
class child extends Parent{}
Here, the “child” is the drive class, and “Parent” is the base class.
Now, take an example to understand the concept of the “extends” keyword:
class Student_informatiom{
int reg_no;
String name;
public Student_informatiom(){}
public Student_informatiom (int reg_no, String name){
this.reg_no = reg_no;
this.name = name;
}
public void display_information() {
System.out.println("Registration number: " + reg_no);
System.out.println("Name: " + name);
}
}
class Student extends Student_informatiom {
int age;
public Student(int reg_no, String name,int age ){
super(reg_no,name);
this.age = age;
}
public void display_information() {
super.display_information();
System.out.println("Age: " + age);
}
public static void main(String[] args) {
System.out.println("------------------Student 1------------------");
System.out.println(""); Student s1 = new Student(101,"Alex",20);
s1.display_information();
System.out.println("------------------Student 2------------------");
Student s2 = new Student(102,"Sarah",21);
s2.display_information();
System.out.println("------------------Student 3------------------");
Student s3 = new Student(103,"Noah",22);
s3.display_information();
}
}
According to the above-stated code:
- Initially, we have a parent class “Student_information” in which we have two variables “reg_no” and “name” to store the name and registration number of the students and a void type “display_information()” method to display the results.
- Then, we have a drive class “Student” that has an integer type “age” variable to add the age of each student and the “display_information()” method used for showing each student’s information.
- In the drive class, we have accessed the parameters and methods of the parent class to store information.
- Lastly, in the “main()” method, we have created “Student” class objects as “s1”, “s2” and “s3” to store each student’s information and display it.
Output

That’s all, we have discussed the working of the “this” and “extends” keywords in Java.
Conclusion
In Java, the “this” keyword is utilized for specifying the current state of an object. However, the “extends” keyword is utilized to drive new classes from the base(parent) class. Both “this” and “extends are the two reserved keywords in Java. In this guide, we have discussed the purposes of the “this” and “extends” keywords in Java with the help of examples.