Home  >  Article  >  Java  >  Super Keyword in Java

Super Keyword in Java

王林
王林Original
2024-08-30 15:44:39264browse

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]);

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:

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
} }

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
}
}

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
}
}

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:
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