Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

super

Example

Utilizing super to invoke the superclass of Dog (a subclass):

class Animal { // Superclass (parent)
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}
class Dog extends Animal { // Subclass (child)
  public void animalSound() {
    super.animalSound(); // Call the superclass method
    System.out.println("The dog says: bow wow");
 }
}
public class Main {
  public static void main(String args[]) {
    Animal myDog = new Dog(); // Create a Dog object
    myDog.animalSound(); // Call the method on the Dog object
  }
}

Definition and Usage

The super keyword pertains to objects of the superclass (parent).

Its usage aids in disambiguating methods with identical names in superclasses and subclasses.

The primary usage of the super keyword is to resolve ambiguity between methods of superclass and subclass sharing the same name.

To grasp the super keyword effectively, a foundational comprehension of Inheritance and Polymorphism is essential.