app development vector
  • What is a Super Keyword with an Example?

  • Published By:
  • Category: Education
  • Published Date: October 28, 2023
  • Modified Date: October 28, 2023
  • Reading Time: 6 Minutes

Featured Image Caption: App Development Vector

In object-oriented programming, the “super” keyword holds significant importance, particularly in Java.

It provides a way to refer to the superclass or parent class, allowing developers to access and utilize its members within a subclass.

The super keyword plays a crucial role in extending the functionality inherited from the superclass while enabling the subclass to add its own specific features.

Super keyword in Java is vital in object-oriented programming, enabling developers to access superclass members.

This article aims to unravel the concept of the super keyword, explore its significance, and provide examples of its usage. Additionally, we will delve into the relationship between the super keyword and access modifiers in Java.

Understanding the Super Keyword

The super keyword in Java refers to the superclass or parent class of a subclass. It provides a way to access and invoke superclass members (fields, methods, or constructors) from the subclass.

By utilizing the super keyword, developers can extend and enhance the functionality inherited from the superclass while retaining the flexibility to add additional features specific to the subclass.

Access Modifiers in Java

Access modifiers in Java control the visibility and accessibility of classes, methods, and variables. Java offers four access modifiers: public, private, protected, and default (package-private).

These modifiers determine which parts of a program can access a particular class member. Understanding access modifiers is crucial in grasping the nuances of using the super keyword effectively.

Usage of Super Keyword with Access Modifiers

Accessing Superclass Members

The super keyword allows access to superclass members from the subclass, even if the member is declared as private in the superclass. This enables subclasses to leverage the functionality of inherited members while encapsulating them from direct access by other classes.

Invoking Superclass Constructors

In scenarios where a subclass extends a superclass with constructors with arguments, the super keyword can invoke the superclass constructor explicitly. This ensures that the initialization logic of the superclass is executed before the subclass-specific logic.

Example: Super Keyword in Action

To illustrate the usage of the super keyword and its relationship with access modifiers, let’s consider a scenario involving a superclass named “Vehicle” and a subclass named “Car.”

public
class Vehicle {
 protected
  String brand;

 public
  Vehicle(String brand) { this.brand = brand; }

 protected
  void drive() { System.out.println("Driving a vehicle"); }
}

public class Car extends Vehicle {
 private
  int mileage;

 public
  Car(String brand, int mileage) {
    super(brand);
    this.mileage = mileage;
  }

 public
  void displayDetails() {
    System.out.println("Brand: " + super.brand);
    System.out.println("Mileage: " + mileage);
  }

 public
  void drive() {
    super.drive();
    System.out.println("Driving a car");
  }
}

In this example, the Car class extends the Vehicle class. The Car constructor uses the super keyword to invoke the superclass constructor and initialize the brand attribute.

The displayDetails() method in the Car class demonstrates accessing the brand attribute of the superclass using the super keyword.

Furthermore, the drive() method showcases invoking the superclass method before executing the subclass-specific implementation.

What is difference between super () and super keyword?

The terms “super()” and “super keyword” both refer to the usage of the “super” keyword in Java, but they have different meanings and contexts. Here’s a breakdown of the difference between the two:

Super()

super() is used to explicitly invoke the constructor of the superclass within the subclass.

It is used to initialize the superclass’s state before initializing the subclass’s state.

It must be the first statement in the subclass constructor and can only be used within a constructor.

By using super(), you can pass arguments to the superclass constructor if it has parameterized constructors.

Example:

public
class Superclass {
 public
  Superclass() {
    // Superclass constructor
  }
}

public class Subclass extends Superclass {
 public
  Subclass() {
    super();  // Invoking superclass constructor
    // Subclass-specific initialization
  }
}

Super keyword

The super keyword is used to refer to the superclass members (fields, methods, and constructors) within the subclass.

It can be used in various contexts, such as accessing superclass fields, invoking superclass methods, or invoking superclass constructors.

It can be used within instance methods of the subclass, including constructors and regular methods.

Example 1 – Accessing Superclass Field:

public
class Superclass {
 protected
  int number;
}

public class Subclass extends Superclass {
 public
  void displayNumber() {
    System.out.println(
        super.number);  // Accessing superclass field using super keyword
  }
}

Example 2 – Invoking Superclass Method:

public
class Superclass {
 public
  void displayInfo() { System.out.println("Hello from superclass"); }
}

public class Subclass extends Superclass {
 public
  void displayInfo() {
    super.displayInfo();  // Invoking superclass method using super keyword
    System.out.println("Hello from subclass");
  }
}

In summary, “super()” refers specifically to the usage of invoking the superclass constructor, while the “super” keyword has a broader usage to refer to superclass members within the subclass.

Can we use super keyword in function?

No, the “super” keyword cannot be used in functions or static methods in Java.

The “super” keyword is used to refer to the superclass members (fields, methods, and constructors) within the context of a subclass.

It is specifically used to access and invoke superclass members from within instance methods or constructors of the subclass.

Since functions and static methods are not associated with a specific instance of a class, they do not have access to instance-specific information, including the “super” keyword.

The “super” keyword is applicable only within the instance context of a subclass to reference its immediate superclass.

Therefore, you can use the “super” keyword only within instance methods and constructors, and not within functions or static methods.

Can we use super keyword without inheritance?

No, the “super” keyword cannot be used without inheritance in Java.

The purpose of the “super” keyword is to reference and access the members of the immediate superclass within a subclass. In Java, inheritance establishes an “is-a” relationship between classes, allowing a subclass to inherit the attributes and behaviors of its superclass.

The “super” keyword is used specifically in the context of inheritance to differentiate between superclass and subclass members with the same name, access superclass constructors, or invoke superclass methods.

It provides a way to explicitly refer to superclass members from within the subclass and extend or override their functionality.

Without inheritance, there is no superclass-subclass relationship, and hence, the “super” keyword is not applicable.

In non-inheritance scenarios, where classes are not related through inheritance, there is no superclass to reference using the “super” keyword.

Conclusion

The super keyword in Java is a powerful tool that facilitates utilizing superclass members within subclasses. It allows for code reuse, inheritance, and the extension of functionality while respecting encapsulation.

Furthermore, understanding access modifiers is crucial for using the super keyword effectively, as it enables appropriate access to superclass members.

By Akshay Parashar
– A professionally trained Tech Expert, with great experience in Data Science, SQL, Machine Learning, Python, and Deep Learning.

Member since December, 2022
View all the articles of Akshay Parashar.

Like it? Share it!

FacebookTwitter / XLinkedInPin ItBufferRedditEmailWhatsapp

Do You Enjoy Writing and Have Something Interesting to Share?

You are at the right place. Inspiring MeMe is the world's fastest growing platform to share articles and opinions. We are currently accepting articles, blogs, personal experiences & tips and would love to have you onboard.

Share your article today!
alert

All images and content mentioned herewith have been shared by the authors/contributors as on dated October 28, 2023. We do not hold any liability for infringement or breach of copyright of third parties across the spectrum. Pictures shared by authors/contributors are deemed to be authorized by them likewise. For any disputes, we shall not be held responsible.

Previous

Making the Transition to University for Military Families

Next

Savoring the Festive Delights of Christmas Markets: Exploring Holiday Flavors

Leave a Reply

© 2015-2024 Inspiring MeMe | All rights reserved.