Super Keyword
Super Keyword in Java
The super keyword in Java is a reference variable that is used to refer to immediate parent class objects.
Usage of Java Super Keyword
- super can be used to refer immediate parent class instance variable.
- super can be used to invoke the immediate parent class method.
- super() can be used to invoke the immediate parent class constructor.

The super
keyword in Java is used in various contexts to interact with a superclass from within a subclass. Here are the different ways super
can be used:
super
keyword in Java is used in various contexts to interact with a superclass from within a subclass. Here are the different ways super
can be used:1. Accessing Superclass Constructor
You can use super()
to call the constructor of the superclass. This is useful for initializing fields in the superclass.Example:class Person {
String name;
int age;
// Constructor with parameters
Person(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
class Student extends Person {
String major;
// Constructor with parameters, including parameters for the superclass
Student(String name, int age, String major) {
super(name, age); // Calls the Person(String, int) constructor
this.major = major;
}
void display() {
super.display(); // Calls the display method of Person
System.out.println("Major: " + major);
}
}
public class ConstructorMain {
public static void main(String[] args) {
Student s = new Student("Alice", 20, "Computer Science");
s.display(); // Output: Name: Alice
// Age: 20
// Major: Computer Science
}
}
super()
to call the constructor of the superclass. This is useful for initializing fields in the superclass.class Person {
String name;
int age;
// Constructor with parameters
Person(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
class Student extends Person {
String major;
// Constructor with parameters, including parameters for the superclass
Student(String name, int age, String major) {
super(name, age); // Calls the Person(String, int) constructor
this.major = major;
}
void display() {
super.display(); // Calls the display method of Person
System.out.println("Major: " + major);
}
}
public class ConstructorMain {
public static void main(String[] args) {
Student s = new Student("Alice", 20, "Computer Science");
s.display(); // Output: Name: Alice
// Age: 20
// Major: Computer Science
}
}
2. Accessing Superclass Methods
You can use super.methodName()
to call a method defined in the superclass that has been overridden in the subclass.
super.methodName()
to call a method defined in the superclass that has been overridden in the subclass.Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
super.sound(); // Calls the sound method of Animal
System.out.println("Dog barks");
}
}
public class MethodsMain {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // Output: Animal makes a sound
// Dog barks
}
}
3. Accessing Superclass Fields
You can use super.fieldName
to access a field of the superclass when it is hidden by a field in the subclass.
super.fieldName
to access a field of the superclass when it is hidden by a field in the subclass.Example:
class Vehicle {
String type = "Vehicle";
}
class Car extends Vehicle {
String type = "Car";
void printType() {
System.out.println("Type: " + super.type); // Accesses the type field of Vehicle
}
}
public class FieldsMain {
public static void main(String[] args) {
Car c = new Car();
c.printType(); // Output: Type: Vehicle
}
}
4. Superclass Method Overloading
If the superclass has multiple overloaded methods, super
can be used to specify which one to call.Example:class Device {
void activate() {
System.out.println("Device is activating");
}
void activate(String deviceName) {
System.out.println(deviceName + " is activating");
}
}
class Phone extends Device {
void activate() {
super.activate("Phone"); // Calls the activate(String deviceName) method of Device
}
}
public class MethodOverloadingMain {
public static void main(String[] args) {
Phone p = new Phone();
p.activate(); // Output: Phone is activating
}
}
super
can be used to specify which one to call.class Device {
void activate() {
System.out.println("Device is activating");
}
void activate(String deviceName) {
System.out.println(deviceName + " is activating");
}
}
class Phone extends Device {
void activate() {
super.activate("Phone"); // Calls the activate(String deviceName) method of Device
}
}
public class MethodOverloadingMain {
public static void main(String[] args) {
Phone p = new Phone();
p.activate(); // Output: Phone is activating
}
}
5. Invoking Superclass Version of Overridden Method
When a method in a subclass overrides a method in the superclass, you can still call the superclass version of the method using super
.Example:class Computer {
void start() {
System.out.println("Computer is starting");
}
}
class Laptop extends Computer {
void start() {
super.start(); // Calls the start method of Computer
System.out.println("Laptop is starting");
}
}
public class OverrideMethodMain {
public static void main(String[] args) {
Laptop l = new Laptop();
l.start(); // Output: Computer is starting
// Laptop is starting
}
}
super
.class Computer {
void start() {
System.out.println("Computer is starting");
}
}
class Laptop extends Computer {
void start() {
super.start(); // Calls the start method of Computer
System.out.println("Laptop is starting");
}
}
public class OverrideMethodMain {
public static void main(String[] args) {
Laptop l = new Laptop();
l.start(); // Output: Computer is starting
// Laptop is starting
}
}
Comments
Post a Comment