The difference between "this" and "super" keywords in Java

What is the difference between this and super keywords?

Both are used to access class constructors on the right? Can any of you explain?

+47
java keyword
Oct 26 '10 at 11:52
source share
11 answers

Let's look at this situation.

 class Animal { void eat() { System.out.println("animal : eat"); } } class Dog extends Animal { void eat() { System.out.println("dog : eat"); } void anotherEat() { super.eat(); } } public class Test { public static void main(String[] args) { Animal a = new Animal(); a.eat(); Dog d = new Dog(); d.eat(); d.anotherEat(); } } 

The output will be

 animal : eat dog : eat animal : eat 

The third line prints "animal: eat" because we call super.eat() . If we called this.eat() , it would print like "dog: eat".

+64
Oct 26 '10 at 12:00
source share

super used to access the methods of the base class, and this used to access the methods of the current class.

An extension of the concept, if you write super() , it refers to the constructor of the base class, and if you write this() , it refers to the constructor of the class itself in which you write this code.

+49
Oct 26 '10 at 11:53 on
source share

this is a reference to an object typed as the current class, and super is a reference to an object entered as its parent class.

In the constructor, this() calls the constructor defined in the current class. super() calls the constructor defined in the parent class. A constructor can be defined in any parent class, but it will refer to the one that has been overridden closest to the current class. Calls for other constructors in this way can only be performed as the first line in the constructor.

Call methods work the same way. Calling this.method() calls the method defined in the current class, where super.method() calls the same method as in the parent class.

+15
Oct 26 '10 at 11:57
source share

From your question, I suppose you are really asking about using this and super in the constructor chain; eg.

 public class A extends B { public A(...) { this(...); ... } } 

against

 public class A extends B { public A(...) { super(...); ... } } 

The difference is simple:

  • This form forms a chain to the constructor of the current class; those. in class A

  • The super form chains to the constructor in the immediate superclass; those. in class B

+10
Oct 26 2018-10-26
source share

this refers to the current class reference.

super refers to the parent of the current class (which is called the super keyword).

By doing this , you can access the methods / attributes of the current class (including its own private methods / attributes).

super allows you to access the public / protected method / attributes of the parent (base) class. You cannot see the parent private method / attributes.

+7
Oct 26 2018-10-26
source share

this used to access the methods and fields of the current object. For this reason, it does not make sense in static methods, for example.

super allows you to access non-physical methods and fields in a superclass and access designers only from class constructors.

+3
Oct 26 '10 at 11:55
source share

When writing code, you usually donโ€™t want to repeat yourself. If you have a class that can be constructed with a different number of parameters, a common solution to avoid repetition is to simply call another constructor with default values โ€‹โ€‹in the missing arguments. There is only one annoying restriction for this - it should be the first line of the declared constructor. Example:

 MyClass() { this(default1, default2); } MyClass(arg1, arg2) { validate arguments, etc... note that your validation logic is only written once now } 

As for the super() constructor, again, unlike super.method() access, it should be the first line of your constructor. After that, it is very similar to the this() , DRY (Do not Repeat Yourself) constructors, if the class you are extending has a constructor that does some of what you want, then use it and then continue to build your object. eg:

 YourClass extends MyClass { YourClass(arg1, arg2, arg3) { super(arg1, arg2) // calls MyClass(arg1, arg2) validate and process arg3... } } 

Additional Information:

Even if you don't see this, the default argument constructor always calls super() . Example:

 MyClass() { } 

equivalently

 MyClass() { super(); } 

I see that many have mentioned using this and super keywords for methods and variables - all is well. Just remember that constructors have unique restrictions on their use, the most notable is that they should be the very first instruction of a declared constructor, and you can use only one.

+3
Oct 26 '10 at 12:28
source share

this is a keyword for calling a constructor in the same class (another overloaded constructor)

syntax: this (args list); // compatible with the argument list in another constructor in the same class

super the keyword is used to call the constructor in the superclass.

syntax: super (args list); // compatible with the args list in the superclass constructor.

Example:

 public class Rect { int x1, y1, x2, y2; public Rect(int x1, int y1, int x2, int y2) // 1st constructor { ....//code to build a rectangle } } public Rect () { // 2nd constructor this (0,0,width,height) // call 1st constructor (because it has **4 int args**), this is another way to build a rectangle } public class DrawableRect extends Rect { public DrawableRect (int a1, int b1, int a2, int b2) { super (a1,b1,a2,b2) // call super class constructor (Rect class) } } 
+3
Aug 11 '13 at 4:32
source share

1. use of the keyword SUPER.

If your method overrides one of its superclass methods, you can invoke the overridden method with the super keyword. You can also use super to refer to a hidden field (although hiding fields is not recommended). Consider this class, Superclass:

 public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } 

}

Here is a subclass of the Subclass subclass that overrides printMethod ():

 public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } 

}

Inside the subclass, the simple name printMethod () refers to a declaration in the subclass that overrides the value in the superclass. So, to reference printMethod () inherited from Superclass, Subclass must use a qualified name using super, as shown. Compiling and executing the subclass prints the following:

Printed in a superclass. Printed in a subclass.




2. using the THIS keyword Using this with a field

The most common reason for using this keyword is that the field is obscured by a method or constructor parameter.

For example, the Point class was written as follows:

 public class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; } 

}

but it could be written like this:

 public class Point { public int x = 0; public int y = 0; //constructor public Point(int x, int y) { this.x = x; this.y = y; } 

}

Each constructor argument obscures one of the object's fields - inside the constructor x there is a local copy of the first constructor argument. To access the Point x field, the constructor must use this.x.

Using this with the constructor

Inside the constructor, you can also use this keyword to call another constructor in the same class. This is called an explicit constructor call. Here is another Rectangle class, with a different implementation from the one in the "Objects" section.

 public class Rectangle { private int x, y; private int width, height; public Rectangle() { this(0, 0, 0, 0); } public Rectangle(int width, int height) { this(0, 0, width, height); } public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } 

}

This class contains a set of constructors. Each constructor initializes some or all of the variables of the rectangle element. Constructors provide a default value for any member variable whose initial value is not provided by the argument. For example, a constructor with no arguments calls a constructor with four arguments with four values โ€‹โ€‹of 0, and a constructor with two arguments calls a constructor with four arguments with two values โ€‹โ€‹of 0. As before, the compiler determines which constructor should call based on the number and type of arguments.

If present, calling another constructor should be the first line in the constructor.

+2
Nov 08 2018-12-13T00:
source share

super () and this ()

  • super () - call the constructor of the parent class.
  • this () is a call to the same class constructor.

Note:

  • We can use super () and this () only in the constructor not elsewhere; an attempt to do this will result in a compile-time error.

  • We must save either super () or this () as the first line of the constructor, but NOT both at the same time.

super and this is the keyword

  • super - call the parent members of the class (variables and methods).
  • this - to call the same class members (variables and methods).

NOTE. . We can use both of them anywhere in the class, except for static areas (static block or method), any attempt to do this will lead to a compile-time error.

+2
Nov 03 '17 at 14:20
source share

It is almost like a situation when a person asked a question that everyone sees only one possible answer. However, a person calls each answer a non-answer, so everyone tries to answer the same answer differently, since there is only one way to answer it.

So, one person begins with A - this is a class, and B - its subclass. The next person begins with C being a class and B its subclass. The third person answers it in a slightly different way. A is a class, and B is a continuation of A. Then the fourth says that A is a class A, which expands as B is defined. Or Animal is a class, and Dog is a subclass. Or a nation is a class, and China is a special case of a nation.

Or better yet, Man is a class, and Clark Kent is a subclass of Man. So Superman ... no ... this does not work in terms of Java ....

Well, this is at least one more attempt to come up with a different description that no one has come up with that can explain things differently. Arggghhhh.

It seems to me that all the explanations worked, except mine.

-one
Sep 09 '15 at 22:38
source share



All Articles