Home > Java > javaTutorial > body text

What are the java keywords super and this? What's the difference?

青灯夜游
Release: 2018-11-21 14:13:07
Original
5722 people have browsed it

The content of this article is to introduce the java keywords super and this? What's the difference? , let everyone understand the role of the keywords super and this, the difference between the keywords super and this, and a brief introduction to superL() and this(). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What are the keywords super and this? What's the difference?

this keyword

this is a reserved keyword in java, i.e. we cannot use it as an identifier.

this: Represents the current object, which allows access to the methods/properties of the current class (including its own private methods/properties).

Simply put: you can access and operate all properties and methods in the current class as well as properties and methods inherited from the parent class that can be allowed by the access modifier.

Example:

class RR { 
	int a = 10; 
	static int b = 20; 

	void GFG() { 
		this.a = 100; 
		this.b = 600; 
		System.out.println(a); 
		System.out.println(b); 
	} 

	public static void main(String[] args) { 
		new RR().GFG(); 
	} 
}
Copy after login

Run result:

What are the java keywords super and this? Whats the difference?

Note: this can only be used in non-static methods of the class, static methods and This must not appear in static code blocks.

super keyword

Super is a reserved keyword in java, i.e. we cannot use it as an identifier.

super represents the parent class object part of the current object class and is used to reference shielded member variables and member methods. Allows access to public (protected) methods or properties, but not to the parent's private methods or properties.

super cannot operate on the properties and methods of this class; it can operate on the properties and methods of the parent class that are allowed by the parent class access modifier, and can only be used when the effect before being overridden is called in this class. super. method.

The purpose of using super is to access the blocked members in the direct parent class. Note that it is the direct parent class (that is, the nearest superclass above the class).

Example:

class Parent { 
	// instance variable 
	int a = 10; 

	// static variable 
	static int b = 20; 
} 
class Base extends Parent { 
	void rr() { 
		System.out.println(super.a); 
		System.out.println(super.b); 
	} 

	public static void main(String[] args) { 
		new Base().rr(); 
	} 
}
Copy after login

Run output:

What are the java keywords super and this? Whats the difference?

##Difference summary:

this key Word: represents the current object, that is, the current class object, calling the current class members (variables and methods);

super keyword: represents the parent class of the current class, calling the members (variables and methods) of the parent class ;

The scope of use of this. is wider than that of super..

A brief introduction to this() and super()

1. This() and super() are both used and can only be used in In the constructor method,

2, this() calls the constructor of this class. If there are formal parameters in the brackets, the constructor of this class with corresponding parameters is called; super() calls the constructor specified by the parent class. Constructor methods,

3, can only be written in the first sentence of the constructor method;

4, super() does not need to be written, and when not written, the super() method of the parent class will be called by default. Parameter construction,

5. this() and super() cannot coexist, and only one of them can be used.

The above is the entire content of this article, I hope it will be helpful to everyone's study. More related video tutorial recommendations:

java tutorial!

The above is the detailed content of What are the java keywords super and this? What's the difference?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template