DEV Community

Sabitha
Sabitha

Posted on

keywords (This and Super)

what is keywords in java?

Keywords in Java are reserved words that have a predefined meaning to the Java compiler. They form the core of the language syntax and are used to define the structure and behavior of Java programs. These words cannot be used as identifiers (like variable or method names). Keywords are case-sensitive and must be spelled exactly as they are written.

Now let’s look into the this and super keywords.

The this and super keywords in Java are reference variables used to access members of a class or its parent class

THIS KEYWORD

  1. Refers to the current object or instance of a class.

  2. Used to access instance variables, methods, and constructors within the same class.

  3. Helps differentiate between instance variables and method parameters with the same name.

  4. Can be used to call the constructor of the same class using this().

Advantages of this Keyword in Java

  1. Clarity
  2. Constructor Chaining
  3. Method Chaining
  4. Passing Itself

SUPER KEYWORD

The super keyword in Java is a reference variable that is used to refer to the parent class when we are working with objects. You need to know the basics of Inheritance and Polymorphism to understand the Java super keyword.

  1. Refers to the superclass or parent class of the current class.

  2. Used to access members (variables and methods) of the superclass.

  3. Used to call the constructor of the superclass using super().

  4. Useful when a subclass overrides a method from the superclass, allowing the subclass to still call the superclass's implementation.

Advantages of Super Keyword in Java

  1. Enable reuse of code
  2. Supports polymorphism
  3. Improving Code Readability

Top comments (0)