What Does Super in Java Do?
In Java, the super keyword is used to access the members of a superclass and invoke its methods. It is used to create a subclass that builds upon the functionality of its superclass. In this article, we will delve into the world of Java’s super keyword and explore its various uses, advantages, and best practices.
1. Accessing Superclass Members
The super keyword is used to access the members of a superclass. Here is an example:
public class Parent {
public void method() {
System.out.println("Parent class");
}
}
public class Child extends Parent {
@Override
public void method() {
System.out.println("Child class");
super.method(); // Calls method() from Parent class
}
}
In this example, the Child class extends the Parent class and overrides the method() method. By using the super keyword, the Child class can access the method() method from the Parent class and call it.
2. Invoking Superclass Constructors
When a subclass is created, it needs to create its own constructor to initialize its instance variables. If the subclass has an overridden constructor, it can also call the superclass constructor using the super() keyword:
public class Parent {
public Parent() {
System.out.println("Parent class");
}
}
public class Child extends Parent {
public Child() {
super(); // Calls Parent class constructor
System.out.println("Child class");
}
}
In this example, the Child class has an overridden constructor and calls the Parent class constructor using the super() keyword.
3. Overriding Superclass Methods
When a subclass wants to override a method from its superclass, it can do so by defining its own method with the same name and signature. The super keyword can be used to call the superclass method from the subclass method:
public class Parent {
public void method() {
System.out.println("Parent class");
}
}
public class Child extends Parent {
@Override
public void method() {
System.out.println("Child class");
super.method(); // Calls method() from Parent class
}
}
In this example, the Child class overrides the method() method from the Parent class and calls it using the super keyword.
4. Using Abstract Classes and Interfaces
Abstract classes and interfaces in Java can be used to define a blueprint for other classes. When an abstract class or interface is implemented, it can override the abstract methods and implement the remaining interface methods:
public abstract class Animal {
public abstract void sound();
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("Woof!");
}
}
In this example, the Dog class implements the Animal class and overrides the sound() method.
5. Providing Default Values for Constructors
In Java, constructors can be used to provide default values for instance variables. The super() keyword can be used to call the superclass constructor with the provided default values:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
}
public class Child extends Person {
public Child() {
super("John"); // Calls Parent class constructor with default values
System.out.println("Child class");
}
}
In this example, the Child class creates an instance of Person with the default value of John and calls the Parent class constructor using the super() keyword.
6. Using the This Keyword
The this keyword can be used to refer to the current object being referred to. This can be useful when using constructors and getters to access instance variables:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class Child extends Person {
public Child() {
System.out.println("Child class");
}
}
In this example, the this keyword is used to refer to the current object being referred to in the getName() method.
7. Generating Accessors and Getters
Generics in Java can be used to define accessors and getters for collections and arrays. The this keyword can be used to refer to the current object being referred to:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
In this example, the getName() method is generated by the compiler and uses the this keyword to refer to the current object being referred to.
8. Using Annotations
Annotations in Java can be used to add metadata to classes, methods, and fields. The super keyword can be used to access the annotation of the superclass:
public class Person {
@Required
private String name;
public Person(String name) {
this.name = name;
}
}
In this example, the @Required annotation is added to the name field in the Person class and can be accessed using the super keyword.
9. Making Methods Static
Methods in Java can be made static by removing the this keyword:
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
}
In this example, the add() method is static and can be called without creating an instance of the MathUtils class.
10. Creating Classes and Interfaces
Classes and interfaces in Java can be created using the class keyword:
public class Employee {
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public void printDetails() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public interface EmployeeInterface {
void printDetails();
}
In this example, a class Employee is created with a constructor and methods, and an interface EmployeeInterface is defined with a single method.
Conclusion
In conclusion, the super keyword in Java is a powerful tool that allows developers to access and manipulate the members of a superclass. By using the super keyword, developers can create subclasses that build upon the functionality of their superclass, and they can also override methods from their superclass to provide custom implementations. Additionally, the super keyword can be used to access the constructor and methods of a superclass, and to generate accessors and getters for collections and arrays.
