Demystifying ‘super’ and ‘this’ Keywords in Java: A Comprehensive Guide

Naveen Metta
6 min readDec 5, 2023

Introduction:

In the expansive landscape of Java, a language renowned for its robust object-oriented programming paradigm, two keywords, ‘super’ and ‘this,’ play pivotal roles in shaping class relationships and method invocations. This article embarks on a journey deep into the heart of these keywords, unraveling their intricacies and providing an extensive array of code examples for a comprehensive understanding.

Understanding ‘super’:

The ‘super’ keyword in Java acts as a vital connector between a subclass and its superclass, enabling seamless communication and interaction. By invoking ‘super,’ developers gain access to the members (fields or methods) of the superclass, facilitating code reuse and maintaining the integrity of the inheritance hierarchy.

Invoking Superclass Constructor:
One of the fundamental applications of ‘super’ lies in invoking the constructor of the superclass within the subclass. This is particularly useful when the superclass has a parameterized constructor, and the subclass needs to ensure its proper initialization.

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

class Dog extends Animal {
Dog() {
super(); // invoking superclass constructor
System.out.println("Dog constructor");
}
}

--

--

Naveen Metta
Naveen Metta

Written by Naveen Metta

I'm a Full Stack Developer with 3+ years of experience. feel free to reach out for any help : mettanaveen701@gmail.com

No responses yet