super keyword in Java
The “super” keyword is an essential component of Java programming that enables the interaction between child and parent classes. It has a variety of applications in Java, making it essential to understand programmers. The “super” keyword is essential when working with inheritance and encapsulation in Java programming. Understanding its usage will enable programmers to write more efficient and effective code.
In this article, we will discuss,
- Use of the ‘super’ keyword to call the parent class constructor
- Use of the ‘super’ keyword to call parent class methods
- Use of the ‘super’ keyword to access superclass variables
- Use of the ‘super’ keyword to access superclass variables and methods from nested classes
Use of the ‘super’ keyword to call parent class constructor:
The ‘super’ keyword in Java can be used to call the constructor of the parent class. When a subclass is created, the constructor of the parent class is automatically called before the constructor of the subclass. However, if the parent class has multiple constructors, it is possible to use the ‘super’ keyword to call a specific constructor. This is useful when the subclass needs to inherit some properties from the parent class, but also wants to add some additional functionality. By calling the parent class constructor with the ‘super’ keyword, the subclass can reuse the code of the parent class constructor, and then add its own code to the constructor.
Here is an example code snippet to demonstrate the use of the ‘super’ keyword to call the parent class constructor:
class Animal { private String name; Animal(String name) { this.name = name; } void printName() { System.out.println("Name: " + name); } class Dog extends Animal { private String breed; Dog(String name, String breed) { super(name); this.breed = breed; } void printBreed() { System.out.println("Breed: " + breed); } } public class Main { public static void main(String[] args) { Dog dog = new Dog("Max", "Labrador"); dog.printName(); dog.printBreed(); } }
Output:
Name: Max
Breed: Labrador
In the above code, the Animal class has a constructor that takes a name parameter. The Dog class is a subclass of Animal and has a constructor that takes a name parameter and a breed parameter. In the Dog class constructor, the super keyword is used to call the Animal class constructor with the name parameter.
This initializes the name property of the Animal class. Then, the breed property of the Dog class is initialized. Finally, the printName() and printBreed() methods are called to print the name and breed of the dog.
By using the super keyword to call the constructor of the parent class, the subclass can reuse the code of the parent class constructor and also add its own code to the constructor. This makes it easier to create subclasses that inherit properties from the parent class.
Use of the ‘super’ keyword to call parent class methods:
When a subclass extends a parent class, it inherits all the methods of the parent class. Sometimes, a subclass may want to use the methods of the parent class but also add some extra functionality to them. In such cases, the ‘super’ keyword can be used to call the parent class methods. To call a parent class method using ‘super’, the method must be defined in the parent class and be accessible by the subclass. The ‘super’ keyword is used with the method name to call the parent class method. For example, consider the following code snippet:
class Parent { public void showMessage() { System.out.println("Hello from Parent"); } } class Child extends Parent { public void showMessage() { super.showMessage(); System.out.println("Hello from Child"); } } public class Main { public static void main(String[] args) { Child c = new Child(); c.showMessage(); } }
In this example, the Parent class has a method called showMessage() which prints “Hello from Parent” to the console. The Child class extends the Parent class and overrides the showMessage() method. However, before printing “Hello from Child”, it first calls the showMessage() method of the parent class using the ‘super’ keyword.
When the main method is executed, it creates an instance of the Child class and calls its showMessage() method. The output of the program is:
Hello from Parent
Hello from Child
As we can see, the showMessage() method of the Parent class is called first using the ‘super’ keyword, and then the showMessage() method of the Child class is called. This allows the Child class to add some extra functionality to the method while still using the code of the Parent class.
It is important to note that the ‘super’ keyword can only be used to call the methods of the immediate parent class. If there are multiple levels of inheritance, then the ‘super’ keyword can be used to call the methods of the immediate parent class, but not the grandparent class or any further up the inheritance hierarchy.
In conclusion, the ‘super’ keyword is a powerful tool in Java that allows a subclass to call the methods of its parent class. This is useful when a subclass wants to use the methods of its parent class but also adds some additional functionality to them. By understanding the use of the ‘super’ keyword, Java programmers can write more efficient and effective code.
Use of the ‘super’ keyword to access superclass variables:
In Java, it is possible to declare variables with the same name in both the superclass and subclass. This is known as hiding variables, and it can cause confusion when trying to access the variables from within the subclass. To avoid this, the ‘super’ keyword can be used to access the superclass variables. When a variable is declared with the same name in both the superclass and subclass, the subclass variable hides the superclass variable. This means that if the subclass wants to access the superclass variable, it must use the ‘super’ keyword. The ‘super’ keyword is used with the variable name to access the superclass variable.
For example, consider the following code:
class SuperClass { int x = 10; } class SubClass extends SuperClass { int x = 20; void printX() { System.out.println("SubClass x: " + x); System.out.println("SuperClass x: " + super.x); } } public class Main { public static void main(String[] args) { SubClass obj = new SubClass(); obj.printX(); } }
In this example, the SuperClass has a variable named ‘x’ with a value of 10. The SubClass also has a variable named ‘x’, but with a value of 20. When the ‘printX’ method is called on the SubClass object, it prints the values of both ‘x’ variables using the ‘super’ keyword to access the superclass variable.
Output:
SubClass x: 20
SuperClass x: 10
Here, the output shows that the ‘super. x’ statement accessed the ‘x’ variable of the superclass, which had a value of 10.
Using the ‘super’ keyword to access superclass variables is particularly useful when dealing with large and complex inheritance hierarchies, where there may be multiple levels of superclass variables with the same name. By using the ‘super’ keyword, it becomes easier to identify and access the correct superclass variable.
5. Use of the ‘super’ keyword to access superclass variables and methods from nested classes:
Nested classes have access to the members of the outer class. However, if a nested class defines a member with the same name as a member of the outer class, the nested class member hides the outer class member. In such cases, the ‘super’ keyword can be used to access the hidden members of the outer class.
Similarly, the ‘super’ keyword can also be used to access the members of the superclass from the nested class. This is useful when a subclass has overridden a method or hidden a variable from its superclass, but still wants to access the superclass member.
Example code snippets to demonstrate the use of the ‘super’ keyword in nested classes
Let’s take an example to understand the use of the ‘super’ keyword in nested classes. Consider the following code:
class Outer { int x = 10; class Inner extends Superclass { int x = 20; void print() { System.out.println(x); //prints 20 System.out.println(this.x); //prints 20 System.out.println(super.x); //prints 10 super.method(); //calls the method of the superclass } } } class Superclass { int x = 30; void method() { System.out.println("Method of superclass"); } }
In the above example, we have a nested class Inner inside the class Outer. The nested class extends Superclass, which has a member variable x and a method (). The nested class also defines a member variable x with the same name as the member variable of the outer class.
The print() method of the nested class prints the value of x using three different ways:
- System.out.println(x) prints the value of the member variable of the nested class, which is 20.
- System.out.println(this. x) also prints the value of the member variable of the nested class, which is 20.
- System.out.println(super. x) prints the value of the member variable of the outer class, which is 10.
The super. method() call in the print() method calls the method of the superclass.
Conclusion
In conclusion, the ‘super’ keyword in Java is an important concept for Java programmers to understand. It allows for efficient reuse of code and access to superclass variables and methods in a subclass. The ‘super’ keyword can be used to call the constructor of the parent class, call methods of the parent class, access hidden variables from the superclass, and access superclass variables and methods from nested classes. By understanding the use of the ‘super’ keyword, Java programmers can write more efficient and concise code, making their programs easier to maintain and develop.