Understanding the “super” Keyword, “this()” & “Access Modifiers” in Java

Nikita Chaurasia
Javarevisited
Published in
4 min readMay 11, 2024

When you’re learning Java programming, it’s really important to understand the super keyword. It’s like a special pass that lets you access stuff from the parent class, like its variables, methods, and constructors. Let’s break it down and see how we can use it in different ways.

Understanding the “super” Keyword in Java

In Java, the `super` keyword holds a crucial role in accessing members of a superclass. Its application extends to three main scenarios: accessing superclass variables, invoking superclass methods, and calling superclass constructors. Let’s delve into each of these uses and understand their significance with examples.

1. Accessing Superclass Variables

When a subclass inherits a variable with the same name as one in its superclass, it can cause variable shadowing, where the subclass variable hides the superclass one. To access the superclass variable, the `super` keyword comes into play.

class Father {
protected double balance = 50000;
}
class Son extends Father {
protected double balance = 18000;
public void showBalance() {
System.out.println("Son balance is: " + balance);
System.out.println("Father balance is: " + super.balance);
}
}

In this example, `super.balance` accesses the `balance` variable from the superclass, ensuring no ambiguity.

2. Invoking Superclass Methods

In cases where a subclass method shares the same name as a method in its superclass, the `super` keyword becomes instrumental in invoking the superclass method.

class Super {
public void show() {
System.out.println("Super class show method");
}
}
class Sub extends Super {
public void show() {
super.show();
System.out.println("Sub class show method");
}
}

Here, `super.show()` ensures that the superclass `show()` method gets called before the subclass method is executed.

3. Calling Superclass Constructors (Constructor Chaining)

Java implicitly adds a default constructor if none is specified in a class. The `super()` keyword is automatically inserted as the first line of any constructor if neither `super()` nor `this()` is explicitly stated.

In Java, the this() keyword is used to invoke one constructor from another constructor within the same class. It allows for constructor chaining and facilitates code reuse and organization.

class Parent {
public Parent() {
System.out.println("Parent class constructor");
}
}
class Child extends Parent {
public Child() {
System.out.println("Child class constructor");
}
}

In this case, invoking `new Child()` calls both the superclass and subclass constructors, demonstrating constructor chaining.

Let’s now explore how this() works and its significance in Java programming.

class MyClass {
private int value;

public MyClass() {
this(0); // Invoking parameterized constructor
}

public MyClass(int value) {
this.value = value;
}
}

In this example, the parameterless constructor calls the parameterized constructor using this(0), passing a default value. This way, both constructors share the common initialization logic, ensuring consistency and reducing redundancy.

Benefits of Using “this()”

  1. Constructor Chaining: this() facilitates constructor chaining, enabling seamless invocation of one constructor from another within the same class. This promotes code reuse and ensures consistent initialization across constructors.
  2. Code Organization: By centralizing common initialization logic in one constructor and invoking it from others using this(), code becomes more organized and easier to maintain. It reduces duplication and enhances code readability.
  3. Flexibility: Using this() allows constructors to focus on specific tasks without worrying about repetitive initialization code. It promotes modular design and facilitates future modifications or additions to the class.

Understanding Access Modifiers in Java

Access modifiers in Java describe the accessibility level of classes and their members. Java offers four access modifiers:

1. private

The private access modifier is the most restrictive, limiting access to the same class only. It's commonly used for variables within a class, ensuring encapsulation and data hiding.

2. default

The default access modifier, when no other modifier is specified, allows access within the same package only. It provides a balance between accessibility and encapsulation.

3. protected

The protected access modifier allows access from subclasses, even if they are in different packages, but through inheritance only. It offers greater accessibility compared to default while still maintaining some level of encapsulation.

4. public

The public access modifier imposes no restrictions and allows access from anywhere, making classes and methods accessible across packages without limitations. It promotes wider accessibility for widely used components.

Conclusion

Understanding the `super` keyword in Java is pivotal for effective inheritance and superclass-subclass interactions. It facilitates seamless access to superclass members, methods, and constructors, ensuring code clarity and flexibility.

The ‘this()’ keyword in Java provides a convenient mechanism for constructor invocation within the same class, promoting code reuse, organization, and flexibility.

By understanding and appropriately utilizing ‘access modifiers’, developers can control the visibility and accessibility of their classes and members, ensuring proper encapsulation and maintaining code integrity.

Thanks for reading

👏 Clap for the story and follow me

📰 Read more content on my Medium

--

--