> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-keywords/super.md).

# super

In Java, the `super` keyword is used to refer to the superclass (parent class) of the current object instance. The `super` keyword in Java is a reference to the superclass and is used to access superclass members, constructors, and methods from within a subclass. It plays an important role in inheritance and allows for better code organization and reuse

## Uses of the `super` keyword

1. **Accessing Superclass Members**: We can use `super` to access methods and instance variables of the superclass within a subclass. This is useful when a subclass overrides a method or hides an instance variable of the superclass and you still want to access the superclass version.

```java
class Parent {
    int value = 10;
    void display() {
        System.out.println("Parent's value: " + value);
    }
}

class Child extends Parent {
    int value = 20;
    void display() {
        System.out.println("Child's value: " + value);
        System.out.println("Parent's value: " + super.value); // Accessing superclass variable
        super.display(); // Invoking superclass method
    }
}
```

2. **Invoking Superclass Constructors**: We can use `super` to invoke constructors of the superclass from a subclass constructor. This is useful when we want to reuse initialization logic defined in the superclass constructor.

```java
class Parent {
    Parent() {
        System.out.println("Parent constructor");
    }
}

class Child extends Parent {
    Child() {
        super(); // Invoking superclass constructor
        System.out.println("Child constructor");
    }
}
```

**Accessing Superclass Methods**: We can use `super` to invoke methods of the superclass even if they are overridden in the subclass.

```java
class Parent {
    void display() {
        System.out.println("Parent's display method");
    }
}

class Child extends Parent {
    @Override
    void display() {
        super.display(); // Invoking superclass method
        System.out.println("Child's display method");
    }
}
```

{% hint style="info" %}
**If a class is not subclassed by any other class, then defining a default constructor (constructor with no arguments) is not mandatory in Java.**

Here's why:

* **Compiler-Generated Default Constructor:** The Java compiler automatically generates a default constructor for any class that doesn't explicitly define one. This default constructor simply calls the no-argument constructor of the superclass (if there is one) and doesn't perform any additional initialization.
* **No Subclasses, No Need for Explicit Call:** Since the class isn't subclassed, there's no need for subclasses to explicitly call a constructor using `super()`. The compiler-generated default constructor will suffice for creating objects of that class.
  {% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-keywords/super.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
