Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between this and super in Java

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the difference between this and super in Java". In daily operation, I believe that many people have doubts about the difference between this and super in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between this and super in Java?" Next, please follow the editor to study!

1. Introduction

This and super are the openers to the Object class channel when the class is instantiated; this and super are ignored by us because they are often implicitly used in the program, but it is certainly necessary to understand their function and usage specification.

2. Introduction

First, let's take a look at two pieces of code, an example of error-free code:

Package com.liziba.ts;/** *

* parent class sample code *

* * @ Author: Liziba * / public class Father {private String name; public String getName () {return name;}} package com.liziba.ts;/** *

* sample code for subclasses *

* @ Author: Liziba * / public class Son extends Father {public Son (String name) {}}

At this point, change the constructor in the Father class to the constructor with parameters

An example of code with errors:

Package com.liziba.ts;/** *

* parent class sample code *

* * @ Author: Liziba * / public class Father {private String name; public Father (String name) {this.name = name;} public String getName () {return name;}

The code of the subclass is not modified, and an error is reported in this case:

This is the error caused by the implicit use of super (). The specific reason is that the constructor of the parent class is implicitly called in the constructor of the subclass Son, which is equivalent to the implicit super (). The code reported above is equivalent to the one below.

However, because the parent class does not have a displayed declaration no-parameter constructor, the no-parameter constructor is overwritten by the parameter constructor, and all super () calls cannot reach the parent class.

There are two solutions at this time:

2.1 declare no-parameter constructor public class Father {public String name; / / parent class what no-parameter constructor public Father () {} public Father (String name) {this.name = name;}} public class Son extends Father {public Son (String name) {super () / / can omit the parameter constructor public class Son extends Father {public Son (String name) {/ / call the parameter constructor super (name) of the parent class displayed by the subclass to call the parent class through super;}}

Next, we will analyze the role and difference of this and super in detail.

3 、 this

This is equivalent to an instance of the current object, or a reference to the current object

This has the following functions:

Call methods and properties in the current object

Distinguish between object properties and method parameters

Call the constructor (must be on the first line of the constructor)

This is equivalent to the current object instance, for example:

Public class Son extends Father {private String homework = "Java programming ideas"; public void doSomething () {/ / this is equivalent to the current Son object instance synchronized (this) {}

Call the methods and properties in the current object for example:

Public class Son extends Father {private String homework = "Java programming ideas"; public void doSomething () {/ / this gets the properties of the current object String hn = this.homework; / / this calls the current object's method this.doSomething2 ();} public void doSomething2 () {/ / toDo}}

Distinguish between object properties and method parameters, for example:

Public class Son extends Father {private String homework = "Java programming ideas"; public Son (String homework) {/ / distinguishes object attributes from method formal parameters this.homework = homework;}}

Call other constructors for example:

Public class Son extends Father {private String homework = "Java programming ideas"; public Son (String homework) {/ / calls other constructors, which must be this on the first line (homework, "you are all architects in the future");} public Son (String homework, String name) {}}

4 、 super

Super can be understood as a reference to an object of a parent class (direct parent class, if there is a multi-tier inheritance relationship here refers to the nearest parent class). Super has the following functions:

Call properties and methods that are not private to the parent class

Distinguish the properties and methods of the current class with the same name as the parent class

Call the constructor of the parent class (must be on the first line of the constructor)

Example of calling parent class properties and methods:

/ * parent class * / public class Father {public String name; public void doSomething3 () {/ / toDo} public void doSomething4 () {/ / toDo}} / * subclass * / public class Son extends Father {public void doSomething () {/ / call the parent class's non-private method super.doSomething3 (); super.doSomething4 () / / call the non-private attribute of the parent class String name = super.name;}}

Examples of properties and methods that distinguish the current class with the same name as the parent class:

/ * parent class * / public class Father {public String name; public void doSomething3 () {/ / toDo} public void doSomething4 () {/ / toDo}} / * subclass code is modified to the following * / public class Son extends Father {public String name; public void doSomething () {/ / super to distinguish the parent method from the method doSomething3 () of the current object DoSomething4 (); super.doSomething3 (); super.doSomething4 (); / / distinguishes the attribute of the current parent class from the attribute of the current class String fatherName = super.name; String sonName = name;} @ Override public void doSomething3 () {/ / todo} @ Override public void doSomething4 () {/ / todo}}

Call the constructor of the parent class (must be on the first line of the constructor)

/ * parent class * / public class Father {public String name; public Father (String name) {this.name = name;}} / * subclass * / public class Son extends Father {public Son (String name) {super (name);}}

5. Summary 5.1 Comparative differences

Basic concepts of this:

Access the instance properties and methods of this class

Basic concepts of super:

Access parent class instance properties and methods

This look in:

Find this class first, and then find the parent class if it doesn't exist.

Super look in:

Find the parent class directly

Other this features:

Use alone to represent the current object

Other super features:

The subclass overrides the parent method, which is used to access the parent method with the same name

5.2 similarities

They are all keywords that act as references.

Other constructors must be called on the first line in the constructor

At this point, the study on "what is the difference between this and super in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 277

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report