Java Programming Handbook

    this vs super Keyword in Java

    When working with Java classes, two important keywords, this and super, help in handling instance variables and inheritance. In this blog, we will understand these keywords with explanations and examples.

    What is this Keyword?#

    The this keyword refers to the current instance of a class. It is mainly used to differentiate between instance variables and parameters with the same name, invoke constructors within the same class, and pass the current instance to a method.

    Uses of this Keyword#

    • Referring to instance variables
    • Calling another constructor in the same class
    • Passing the current object as a parameter
    • Returning the current instance from a method

    Example: Using this to refer to instance variables#

    class Student { String name; int age; // Constructor Student(String name, int age) { this.name = name; // 'this' differentiates instance variable and parameter this.age = age; } void display() { System.out.println("Name: " + this.name + ", Age: " + this.age); } } public class Main { public static void main(String[] args) { Student s1 = new Student("John", 22); s1.display(); } }

    Output:#

    Name: John, Age: 22

    Example: Using this to call another constructor#

    class Person { String name; int age; // Default constructor Person() { this("Unknown", 0); // Calls parameterized constructor } // Parameterized constructor Person(String name, int age) { this.name = name; this.age = age; } void display() { System.out.println("Name: " + name + ", Age: " + age); } } public class Main { public static void main(String[] args) { Person p1 = new Person(); p1.display(); } }

    Output:#

    Name: Unknown, Age: 0

    What is super Keyword?#

    The super keyword is used in inheritance to refer to the parent class. It is mainly used to access parent class members, invoke parent class methods, and call parent class constructors.

    Uses of super Keyword#

    • Accessing parent class methods
    • Calling the parent class constructor
    • Accessing parent class variables

    Example: Using super to call parent class methods#

    class Animal { void makeSound() { System.out.println("Animals make sound"); } } class Dog extends Animal { void makeSound() { super.makeSound(); // Calls the makeSound() of Animal class System.out.println("Dogs bark"); } } public class Main { public static void main(String[] args) { Dog d = new Dog(); d.makeSound(); } }

    Output:#

    Animals make sound Dogs bark

    Example: Using super to call parent class constructor#

    class Parent { Parent() { System.out.println("Parent class constructor"); } } class Child extends Parent { Child() { super(); // Calls the parent class constructor System.out.println("Child class constructor"); } } public class Main { public static void main(String[] args) { Child c = new Child(); } }

    Output:#

    Parent class constructor Child class constructor

    Difference Between this and super#

    Featurethissuper
    Refers toCurrent instance of the classParent class instance
    Used forAccessing instance variables, methods, and constructorsAccessing parent class members and constructors
    CallsAnother constructor in the same classParent class constructor
    Can be used in static methods?NoNo

    Conclusion#

    In this blog, we learned about the this and super keywords in Java. The this keyword is used for referring to the current instance, differentiating instance variables, and calling constructors within the same class. The super keyword is used to refer to the parent class, access its members, and call its constructor. Understanding these concepts is crucial for working with object-oriented programming and inheritance in Java.

    Last updated on Apr 09, 2025