Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In Java, the super and this keywords are important when working with classes and objects. They help manage inheritance and refer class members.
The super keyword is used to refer to the parent class, making it useful for accessing parent methods and constructors. On the other hand, this is used to refer to the current class instance, helping in situations where there are naming conflicts between variables or when calling one constructor from another.
In this blog, we will explore how super and this work, their differences, and how they make Java programming easier.
Why Are They Important in Java Programming?
The keywords this and super play a key role in writing clear, maintainable, and reusable Java code.
The this keyword refers to the current object. It helps avoid naming conflicts, especially when method parameters or constructors have the same name as class fields. For example:
this.name = name;
This improves code readability and ensures the correct variable is being referenced.
The super keyword refers to the parent class. It's useful for accessing overridden methods, calling superclass constructors, or accessing inherited fields. This is especially helpful in method overriding and constructor chaining:
super.display();
super(name);
By using this and super properly, developers can write more structured and reusable code, particularly when working with inheritance and class hierarchies.
“Super” Keyword in Java
In Java, the super keyword is a reference variable that refers to the immediate superclass/parent class objects if using the keyword inside the child class.
Uses of super Keyword in Java
Access Parent Class Variables: When a child class has a variable with the same name as its parent class, super helps access the parent’s variable.
Call Parent Class Methods: If a child class overrides a method from its parent, super allows calling the parent class’s version of the method.
Invoke Parent Class Constructor: super() can be used to call the parent class constructor from a child class constructor.
Differentiate Between Parent and Child Class Members: It helps when both parent and child classes have members with the same name.
Improve Code Reusability: By using super, we can reuse parent class functionalities instead of rewriting code in the child class.
Examples of super Keyword in Java
To understand how super works, let's look at a few examples. These examples will show how super helps in accessing parent class methods, variables, and constructors.
Refer To Immediate Superclass Instance Variable
the super keyword is used to access the superclass/ parent class field provided parent and child class have the same fields.
For example
Java
Java
class Father{ int age=47; } class Child extends Father{ int age=20; void displayAge(){ System.out.println(“Age of Child is ”+age);//prints marks of Student1 class System.out.println(“Age of Father is ”+super.age); //prints marks of Student2 class } } class Example7{ public static void main(String args[]){ Child c=new Child(); c.displayAge(); } }
You can also try this code with Online Java Compiler
super keyword can be used to invoke the parent class constructor.
For example
Java
Java
class Father{ Father(){ System.out.println("Age of Father is 47"); } } class Child extends Father{ Child(){ super(); System.out.println("Age of Child is 20"); } } class Example8{ public static void main(String args[]){ Child c=new Child(); } }
You can also try this code with Online Java Compiler
In Java, this keyword is a reference variable in a method or constructor that refers to the current object.
Java
Java
class Example { int value; Example (int value){ this.value = value; System.out.println("this = " + this); } public static void main(String[] args) { Example demo = new Example (49); System.out.println("demo object = " + demo); } }
You can also try this code with Online Java Compiler
In the example above, we created an object, demo, of the class Example. this keyword of the class is then printed, along with reference to the object demo.
Here, we can see that both demo object and this have the same reference. It implies that this is a reference to the current object.
Uses of this Keyword in Java
Referring to Instance Variables: It helps when local and instance variables have the same name by differentiating them.
Calling Another Constructor: The this() keyword can be used to call one constructor from another within the same class.
Calling Current Class Methods: It allows calling a method of the same class without creating an explicit object.
Returning the Current Object: this can be used to return the current class instance from a method.
Passing the Current Object as a Parameter: It helps in passing the current object as an argument to another method or constructor.
Examples of this Keyword in Java
To understand the this keyword better, let’s look at a few examples. These examples will demonstrate how this helps in handling variable shadowing, constructor chaining, and method calls.
Refer Current Class Instance Variable
this keyword is mainly used to differentiate between the formal parameters and instance variables having the same name.
class Example1{ public static void main(String args[]){ Mobile mob1=new Mobile("Samsung", “GalaxyA23”); Mobile mob2=new Mobile("Apple", “iPhone14”); mob1.show(); mob2.show(); } }
You can also try this code with Online Java Compiler
this keyword helps in constructor reusing and accomplishing constructor chaining.
For example
Java
Java
class Demo{ Demo(){ System.out.println("This is a demo constructor"); } Demo(String temp){ this(); System.out.println(temp); } } class Example2{ public static void main(String args[]){ Demo obj=new Demo(“Demo for this keyword”); } }
You can also try this code with Online Java Compiler
For invoking a current class method, this keyword is used. However, if the user does not add this keyword, the compiler automatically adds it.
For example:
Java
Java
class Calculator{ void addition(){ System.out.println("Addition of 4 and 3 is " + 7); } void multiplication(){ System.out.println("Multiplication of 4 and 3 is " + 12); this.addition(); // this.addition() and addition() is same } } class Example3{ public static void main(String args[]){ Calculator calc=new Calculator(); calc.multiplication(); } }
You can also try this code with Online Java Compiler
The super keyword allows a subclass to call a method defined in its parent class, even if it’s overridden. This is useful when you want to extend the parent’s behavior, not completely replace it.
class Animal {
void sound() { System.out.println("Animal makes a sound"); }
}
class Dog extends Animal {
void sound() {
super.sound(); // Call parent method
System.out.println("Dog barks");
}
}
2. Avoiding Variable Shadowing
When a method parameter has the same name as an instance variable, this helps refer to the class field to avoid confusion or errors.
class Person {
String name;
Person(String name) {
this.name = name; // 'this.name' refers to the instance variable
}
}
3. Managing Inheritance Effectively
super helps call a parent class’s constructor, allowing subclasses to reuse and extend initialization logic. Think of it as inheriting the foundation before adding new floors.
class Vehicle {
Vehicle(String type) { System.out.println("Vehicle type: " + type); }
}
class Car extends Vehicle {
Car() {
super("Car"); // Call Vehicle constructor
System.out.println("Car created");
}
}
Frequently Asked Questions
Should I use super or this?
Use super to refer to the parent class and this to refer to the current class. Choose based on whether you need parent or current class members.
Is super() automatically called?
Yes, super() is automatically called in a child class constructor if no other constructor call (super() or this()) is explicitly written.
Is super() called by default?
Yes, if a child class constructor does not explicitly call super() or this(), Java implicitly inserts super() to call the parent class’s no-argument constructor.
Can we use both this() and super() in the same constructor?
No, both this() and super() must be the first statement in a constructor, so they cannot be used together in the same constructor.
Why this and super cannot be used together?
Both this() and super() must be the first statement in a constructor, and Java does not allow two first statements in the same constructor.
Conclusion
Understanding the super and this keywords in Java is essential for writing clean, efficient, and maintainable code. The super keyword helps in accessing parent class members, calling overridden methods, and invoking parent class constructors, making it useful for working with inheritance. On the other hand, the this keyword is used within the same class to refer to instance variables, call constructors, and pass the current object.
You can refer to similar articles for more information