Java this/super

Java this and super keywords

Welcome to a tutorial that will unravel the mysteries of the 'this' and 'super' keywords in Java. These two keywords play a crucial role in object-oriented programming, allowing you to navigate class hierarchies, manage variables, and write more maintainable code.

'this' Keyword: Understanding Self-Reference

  • The 'this' keyword refers to the current instance of the class in which it's used.
  • It's like saying "me" when you want to emphasize that you are referring to yourself.
Example
class Person {
    String name;

    Person(String name) {
        this.name = name; // 'this' refers to the current object
    }

    void introduce() {
        System.out.println("Hello, I am " + this.name);
    }
}

Person person1 = new Person("Alice");
person1.introduce(); // Output: Hello, I am Alice

'super' Keyword: Accessing Parent Class Members

  • The 'super' keyword is used to access members (variables and methods) of the parent class (superclass).
  • It's like calling your parent when you need help or guidance.
Example
class Animal {
    String species;

    Animal(String species) {
        this.species = species;
    }

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

class Dog extends Animal {
    String breed;

    Dog(String species, String breed) {
        super(species); // Calls the parent class constructor
        this.breed = breed;
    }

    void makeSound() {
        System.out.println("Dog barks");
    }
}

Dog myDog = new Dog("Canine", "Golden Retriever");
System.out.println("Species: " + myDog.species); // Output: Species: Canine
myDog.makeSound(); // Output: Dog barks

Using 'this' to Avoid Ambiguity:

  • When subclass and superclass have variables with the same name, 'this' can be used to distinguish between them.
  • It's like telling someone, "I mean my 'name,' not yours."
Example
class Animal {
    String name;

    Animal(String name) {
        this.name = name;
    }
}

class Dog extends Animal {
    String name; // Variable with the same name as the superclass

    Dog(String species, String name) {
        super(species);
        this.name = name; // Use 'this' to refer to the subclass variable
    }

    void introduce() {
        System.out.println("I am a " + super.name + " and my name is " + this.name);
    }
}

Dog myDog = new Dog("Canine", "Buddy");
myDog.introduce(); // Output: I am a Canine and my name is Buddy

Using 'super' to Access Overridden Methods:

  • 'super' can be used to call overridden methods of the parent class.
  • It's like following the traditional family recipe for a dish.
Example
class Vehicle {
    void start() {
        System.out.println("Vehicle started");
    }
}

class Car extends Vehicle {
    @Override
    void start() {
        super.start(); // Calls the start method of the parent class
        System.out.println("Car started");
    }
}

Car myCar = new Car();
myCar.start(); // Output: Vehicle started
               // Car started

Summary:

  • 'this' refers to the current instance of the class.
  • 'super' accesses members of the parent class.
  • 'this' and 'super' help avoid ambiguity and call overridden methods.

Contact Us

Name
Email
Mobile No:
subject:
Message: