Difference between this and super keyword in Java
This post will discuss the difference between this and super keyword in Java. We will also cover the difference between this() and super() method in Java.
1. Overview of this keyword
The this keyword is a reference to the current object as an instance of the current class. It can be used to access the fields and methods of the current class, as well as to invoke the constructors of the current class. The this keyword can be used in four contexts:
1. To access fields of the current class
The this keyword can be used to access the instance variables of the current class, especially when they are shadowed by local variables or parameters with the same name. For example:
|
1 2 3 4 5 6 7 |
class Person { private String name; // Instance variable public Person(String name) { this.name = name; // Use this to refer to the instance variable } } |
2. To invoke constructors of the current class
The this keyword can be used to invoke another constructor of the same class within a constructor. This is useful when there are multiple constructors with different parameters, and some common code needs to be executed by all of them. For example:
|
1 2 3 4 5 6 7 8 9 10 11 |
class Point { // Instance variables private int x; private int y; // Constructor with no parameters public Point() { // Use this to call another constructor with default values this(10, 10); } } |
3. To invoke methods of the current class
The this keyword can be used to invoke the instance methods of the current class. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Person { private String name; // Instance variable public Person(String name) { this.name = name; // Use this to refer to the instance variable } public void sayHello() { System.out.println("Hello " + name); } public void greet() { this.sayHello(); // Invoke sayHello() using this } } class Main { public static void main(String[] args) { Person person = new Person("Anne"); person.greet(); } } |
4. To return current object from a method
The this keyword can be used to return the reference of the current object from a method. This is useful when we want to chain multiple methods on the same object, or when we want to pass the current object as a parameter to another method. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class Person { // Declare two instance variables name and age private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public Person setName(String name) { this.name = name; return this; // Return the current object using this } public Person setAge(int age) { this.age = age; return this; // Return the current object using this } // getters @Override public String toString() { return "{" + name + ", " + age + "}"; } } class Main { public static void main(String[] args) { Person person = new Person("Anne", 15); System.out.println(person); // {Anne, 15} // Chain multiple setter methods on the same object using this person.setName("David").setAge(20); System.out.println(person); // {David, 20} } } |
2. Overview of super keyword
The super keyword is a reference to the current object as an instance of the parent class. It can be used to access the fields and methods of the parent class, as well as to invoke the constructors of the parent class. The super keyword can be used in below contexts:
1. To invoke constructor or access fields of the parent class
The super keyword can be used to invoke the constructor of the parent class from the constructor of the current class. This is useful when we want to initialize some common data or logic in the parent class before initializing the current class. It can also be used to access the instance variables of the parent class, especially when they are hidden by instance variables of the subclass with the same name. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class Vehicle { protected String name; public Vehicle(String name) { this.name = name; } } class Car extends Vehicle { private String model; public Car(String name, String model) { // Use super to call parent class constructor with one parameter super(name); // Use this to assign the parameter to the instance variable of current class this.model = model; } @Override public String toString() { // Use super to access the instance variable of parent class return "{name='" + super.name + "', model='" + this.model + "'}"; } } class Main { public static void main(String[] args) { Car car = new Car("BMW", "X7"); System.out.println(car); } } |
2. To invoke methods of the parent class
The super keyword can be used to invoke the method of the parent class from the method of the current class. This is useful when we want to reuse some common code or logic in the parent class or override some behavior in the current class. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Shape { public void draw() { System.out.println("Parent class method called"); } } class Circle extends Shape { public void draw() { super.draw(); // Use super to call parent class method } } class Main { public static void main(String[] args) { Circle circle = new Circle(); circle.draw(); // Parent class method called } } |
3. Difference between this and super keyword
The functionality of this and super keyword are similar. They both are reference variables that can be used to access the fields, methods, and constructors of a class. However, they also have some differences that need to be understood clearly:
- The
thiskeyword points to the current object of the class, while thesuperkeyword points to the parent object of the current object of the class. - The
thiskeyword can be used to access the fields, methods, constructors, and current object of the current class, while thesuperkeyword can be used to access the fields, methods, and constructors of the parent class from the subclass. - The
thiskeyword can be used in any method or constructor of a class, while thesuperkeyword can be used only in a subclass method or constructor. - The
thiskeyword can be used as an argument to another method or constructor, while thesuperkeyword cannot be used as an argument. - The
thiskeyword can be used as a return value from a method, while thesuperkeyword cannot be used as a return value.
That’s all about the differences between this and super keyword in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)