this and super Keywords in Java
×


this and super Keywords in Java

133

Introduction to this and super Keywords in Java

In Java, this and super are special keywords that help manage references within classes, especially when dealing with inheritance. They provide a way to access the current object and parent class members respectively, making code clearer and more manageable.

Understanding the this Keyword

The this keyword refers to the current instance of the class. It is commonly used to:

  • Distinguish between class fields and parameters when they share the same name.
  • Call another constructor in the same class (constructor chaining).
  • Pass the current object as a parameter.

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;  // 'this.name' refers to instance variable, 'name' is parameter
    }

    public void print() {
        System.out.println("Name: " + this.name);
    }

    public Person() {
        this("Unknown");  // Calling another constructor using 'this'
    }
}

    

Exploring the super Keyword

The super keyword is used to refer to the immediate parent class object. It is often used to:

  • Invoke parent class constructors.
  • Access parent class methods that are overridden in the child class.
  • Access parent class variables when they are hidden by child class variables.

class Animal {
    String sound = "Some sound";

    Animal() {
        System.out.println("Animal constructor called");
    }

    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    String sound = "Bark";

    Dog() {
        super();  // Calls the parent class constructor
        System.out.println("Dog constructor called");
    }

    @Override
    void makeSound() {
        super.makeSound();  // Calls parent class method
        System.out.println("Dog says: " + sound);
        System.out.println("Parent sound: " + super.sound);  // Access parent variable
    }
}

    

Key Differences Between this and super

Aspectthissuper
Reference toCurrent class objectImmediate parent class object
Constructor CallCalls another constructor of the same classCalls parent class constructor
Access MembersAccesses current class fields and methodsAccesses overridden methods and hidden variables from parent class
Usage ContextUsed within instance methods and constructors of the same classUsed within child class methods and constructors


Practical Example Combining this and super


class Vehicle {
    String brand;

    Vehicle(String brand) {
        this.brand = brand;
    }

    void display() {
        System.out.println("Vehicle brand: " + brand);
    }
}

class Car extends Vehicle {
    String model;

    Car(String brand, String model) {
        super(brand);  // Call parent constructor
        this.model = model;  // Refers to current class field
    }

    void display() {
        super.display();  // Call parent method
        System.out.println("Car model: " + model);
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car("Toyota", "Camry");
        car.display();
    }
}

    

Summary

The this and super keywords in Java serve distinct but complementary roles. this helps work with the current object, especially when clarifying field and parameter names or chaining constructors. super is vital for interacting with parent class members and constructors, enabling effective inheritance and polymorphism.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!




Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat