Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, the ‘super‘ keyword is used in the context of inheritance to refer to the superclass (parent class) of the current object. It provides a way to access methods and constructors of the superclass that have been overridden in the subclass. The super keyword serves several important purposes in Java programming, and its usage can be divided into three main categories: calling superclass methods, calling superclass constructors, and accessing superclass fields.

1. Calling Superclass Methods

When a method in a subclass overrides a method in its superclass, the super keyword can be used to call the overridden method from the superclass. This is useful when you want to retain some behavior of the superclass method while extending or modifying it in the subclass.

Example:

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

class Dog extends Animal {
    void makeSound() {
        super.makeSound();  // Call the superclass method
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound();  // Output: Animal makes a sound
                          //         Dog barks
    }
}

				
			

In this example, the makeSound method in the Dog class overrides the ‘makeSound‘ method in the ‘Animal‘ class. By using ‘super.makeSound()‘, the ‘Dog‘ class can call the ‘makeSound‘ method of the ‘Animal‘ class.

2. Calling Superclass Constructors

The ‘super‘ keyword can also be used to call a constructor of the superclass from the constructor of the subclass. This is often necessary when the superclass does not have a default constructor or when specific initialization defined in the superclass needs to be executed

Example:

				
					class Animal {
    Animal(String name) {
        System.out.println("Animal constructor: " + name);
    }
}

class Dog extends Animal {
    Dog(String name) {
        super(name);  // Call the superclass constructor
        System.out.println("Dog constructor: " + name);
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy");  // Output: Animal constructor: Buddy
                                     //         Dog constructor: Buddy
    }
}

				
			

In this example, the Dog class constructor calls the ‘Animal‘ class constructor using ‘super(name)‘. This ensures that the ‘Animal‘ class is properly initialized with the given name before the ‘Dog‘ class adds its own initialization.

3. Accessing Superclass Fields

The ‘super‘ keyword can also be used to access fields of the superclass if they are hidden by fields of the same name in the subclass. This allows the subclass to distinguish between its own fields and those inherited from the superclass.

Example:

				
					class Animal {
    String name = "Generic Animal";
}

class Dog extends Animal {
    String name = "Dog";

    void printNames() {
        System.out.println("Subclass name: " + name);
        System.out.println("Superclass name: " + super.name);  // Access the superclass field
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.printNames();  // Output: Subclass name: Dog
                           //         Superclass name: Generic Animal
    }
}

				
			

In this example, the ‘Dog‘ class has its own ‘name‘ field that hides the ‘name‘field in the ‘Animal‘ class. By using ‘super.name‘, the ‘Dog‘ class can access the ‘name‘ field of the ‘Animal‘ class.

Summary

The super keyword in Java is a powerful feature for managing inheritance and allows subclasses to:

  • Invoke superclass methods that have been overridden.
  • Call superclass constructors to ensure proper initialization.
  • Access superclass fields that are hidden by subclass fields.

Understanding and utilizing the super keyword effectively can help you write more robust and maintainable object-oriented code.

Scroll to Top