Super is a keyword that is used to call a function or method in the superclass. This will be defined inside the sub-class. Methods that are only public and protected can be called by using this keyword. In other words, Private methods and static methods cannot be called using this. The super keyword in java can also be used in order to call the constructors of the parent class. Syntax, examples and further details of super keyword will be discussed in the following sections.
Syntax
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
super.> or super([0 or more arguments]);
How does Super Keyword work in Java?
As already mentioned, super can be used on several occasions.
- To refer instance variable of an immediate parent class.
- To refer to the method of an immediate parent class.
- To refer to the constructor of an immediate parent class.
1. To Refer Instance Variable of an Immediate Parent Class
If the parent and child class have the same data members, the Super keyword can be used to access the field or data member of the parent class. Ambiguity for Java Virtual Machine can occur in this case.
Example:
Code:
class A { protected String name="ann"; } class B extends A { public String name="Anna"; public void hello() { System.out.println("I am " + name); System.out.println("I am " + super.name); } }
Here, two classes A and B, have a common field name. A function printType() inside child class uses the super keyword to refer to the field in a parent class.
2. To Refer Method of An Immediate Parent Class
Method overriding is a process by which a child class declares the same function or method that is already available in the parent class. Suppose, if a call to the method happens from the object of the child class, then the method in the child class only will be called. In order to access the parent method, a super keyword can be used.
Example:
Code:
class A { protected String name="ann"; public void hello() { System.out.println("I am " + name); } } class B extends A { public String name="Anna”; public void hello() { System.out.println("I am " + name); } public void test() { hello(); super.hello(); } }
Here, two classes, A and B, have the same method hello(). With the help of a super keyword in the test() function, it is possible to access the hello() method of a parent class.
3. To Refer Constructor of Immediate Parent Class
It is already known that a constructor (default) gets automatically called when the object of a class is created. The super keyword can be used to explicitly call the constructor of the superclass from the constructor of the subclass. Make sure that super is used only inside the constructor of the subclass, and it is the first statement inside that.
Example:
Code:
class A { //constructor of parent class A() { System.out.println("I am Kavya Madhavan"); } } //child class class B extends A { //constructor of child class B() { super(); System.out.println("I am Dileep Menon"); } }
Examples of Super Keyword in Java
Below are the different examples mentioned:
Example #1
In the following program, a common variable name is present and super is used to call the variable in a parent class.
Code:
//Java program to illustrate Super keyword to refer instance variable //parent class class A { protected String name="ann"; } //child classs class B extends A { public String name="Anna";//variable which is same in parent class //sample method public void hello() { System.out.println("I am " + name); System.out.println("I am " + super.name); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class objb.hello();//call the method in child class } }
Output:
Example #2
This program helps in demonstrating the super keyword while referring to the same method in a parent class. Here, hello() is a method that is available in both classes.
Code:
//Java program to illustrate Super keyword to refer same method in parent class //parent class class A { protected String name="ann"; public void hello() { System.out.println("I am " + name); } } //child classs class B extends A { public String name="Anna";//variable which is same in parent class //sample method which is same in parent class public void hello() { System.out.println("I am " + name); } //method to call the hello() method in parent and child class public void test() { hello(); super.hello(); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class objb.test();//call the method in child class } }
Output:
Example #3
This program calls the constructor of the parent class using the super keyword.
Code:
//Java program to illustrate Super keyword to refer constructor in parent class //parent class class A { //constructor of parent class A() { System.out.println("I am Kavya Madhavan"); } } //child class class B extends A { //constructor of child class B() { super(); System.out.println("I am Dileep Menon"); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class } }
Output:
Example #4
This program demonstrates a super keyword’s usage to refer to the parameterized constructor of a parent class.
Code:
//Java program to illustrate Super keyword to refer parameterised constructor in parent class //parent class class A { //constructor of parent class A() { System.out.println("I am Kavya Madhavan"); } //parameterised constructor A(String name) { System.out.println("I am " + name); } } //child class class B extends A { //constructor of child class B() { super("Renuka"); System.out.println("I am Dileep Menon"); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class } }
Output:
Conclusion
Super is a keyword in Java that is used to refer to the methods or functions, instance variables or attributes and constructors in the parent class. If a constructor is not declared, the compiler automatically creates a default constructor. Similarly, Compiler calls super() automatically if it is not declared. In this document, several aspects of the super keyword are explained in detail.
The above is the detailed content of Super Keyword in Java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools