Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, the super
keyword is used to refer to the immediate parent class of a subclass. It is mainly used for two purposes: to call the superclass's constructor and to access superclass's methods and variables.
When creating a subclass, you may need to call the constructor of the parent class to initialize the inherited fields. You can use the super
keyword followed by the constructor's arguments in parentheses.
Syntax:
super(arg1, arg2, ...);
Example:
class Vehicle { String type; Vehicle(String type) { this.type = type; } } class Car extends Vehicle { int numberOfWheels; Car(String type, int numberOfWheels) { super(type); // Calling the superclass's constructor this.numberOfWheels = numberOfWheels; } } public class Main { public static void main(String[] args) { Car car = new Car("Sedan", 4); System.out.println("Car type: " + car.type); System.out.println("Number of wheels: " + car.numberOfWheels); } }
Output:
Car type: Sedan Number of wheels: 4
The super
keyword can also be used to access methods and variables of the superclass when they are overridden or hidden by the subclass.
Syntax:
super.methodName(); super.variableName;
Example:
class Animal { String sound = "generic sound"; void makeSound() { System.out.println("The animal makes a " + sound); } } class Dog extends Animal { String sound = "bark"; void makeSound() { System.out.println("The dog makes a " + sound); super.makeSound(); // Calling the superclass's method } void displaySuperSound() { System.out.println("The superclass's sound is: " + super.sound); // Accessing the superclass's variable } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); dog.displaySuperSound(); } }
Output:
The dog makes a bark The animal makes a generic sound The superclass's sound is: generic sound
In conclusion, the super
keyword in Java is used to refer to the immediate parent class of a subclass. It allows you to call the superclass's constructor and access its methods and variables when they are overridden or hidden by the subclass.
Java super() constructor call:
The super()
constructor call is used to invoke the constructor of the superclass from the subclass constructor.
Inheritance and super keyword in Java:
Inheritance allows a subclass to inherit properties and behaviors from its superclass. The super
keyword is used to refer to members of the superclass.
Calling superclass methods with super in Java:
The super
keyword can be used to call methods of the superclass from within the subclass.
class Superclass { void display() { System.out.println("Superclass method"); } } class Subclass extends Superclass { void display() { super.display(); // Calling superclass method System.out.println("Subclass method"); } }
Using super in constructor chaining in Java:
super()
is often used in constructor chaining to ensure that the constructors of both the superclass and subclass are called.
class Superclass { Superclass() { System.out.println("Superclass constructor"); } } class Subclass extends Superclass { Subclass() { super(); // Calls Superclass constructor System.out.println("Subclass constructor"); } }
Overriding methods with super in Java:
The super
keyword is often used when overriding methods to call the overridden method in the superclass.
class Superclass { void display() { System.out.println("Superclass method"); } } class Subclass extends Superclass { void display() { super.display(); // Calls Superclass method System.out.println("Subclass method"); } }
Superclass and subclass relationships in Java: Subclasses inherit attributes and behaviors from their superclass, creating an "is-a" relationship.
Java super() vs super.method() usage:
super()
is used to call the superclass constructor, while super.method()
is used to call a specific method from the superclass.
super(); // Calls superclass constructor super.display(); // Calls a specific method from the superclass
Accessing superclass fields with super in Java:
The super
keyword is used to access fields from the superclass within the subclass.
class Superclass { int num = 10; } class Subclass extends Superclass { void display() { System.out.println(super.num); // Accesses the 'num' field from Superclass } }
Java super() in default constructor:
When a subclass constructor doesn't explicitly call super()
, the default constructor of the superclass is implicitly called.
Java super() vs super(arguments) in constructors:
super()
calls the default constructor of the superclass, while super(arguments)
calls a specific constructor of the superclass with provided arguments.
super(); // Calls default constructor of the superclass super(10, "Hello"); // Calls a specific constructor of the superclass with arguments
Dynamic method dispatch and super in Java:
Dynamic method dispatch allows the JVM to determine the appropriate method at runtime, and super
can be used in the context of dynamic method dispatch when dealing with overridden methods.