Super Keyword in Java

王林
リリース: 2024-08-30 15:44:39
オリジナル
195 人が閲覧しました

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:

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.

以上がSuper Keyword in Javaの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!