Table of Contents
How does Super Keyword work in Java?
1. To Refer Instance Variable of an Immediate Parent Class
2. To Refer Method of An Immediate Parent Class
3. To Refer Constructor of Immediate Parent Class
Examples of Super Keyword in Java
Example #1
Example #4
Conclusion
Home Java javaTutorial Super Keyword in Java

Super Keyword in Java

Aug 30, 2024 pm 03:44 PM
java

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.<<method-name>> or super([0 or more arguments]);
Copy after login

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);
}
}
Copy after login

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();
}
}
Copy after login

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");  } }
Copy after login

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
}
}
Copy after login

Output:

Super Keyword in Java

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
} }
Copy after login

Output:

Super Keyword in Java

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
}
}
Copy after login

Output:

Super Keyword in Java

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
}
}
Copy after login

Output:

Super Keyword in Java

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles