Oops Special Keywords in Java

Special keywords in Java are reserved words that have predefined meanings and cannot be used as identifiers or variable names. These keywords serve specific purposes and are an integral part of the Java language.

final

The final keyword is used to declare a constant variable, a method that cannot be overridden, or a class that cannot be subclassed.

Syntax
    
final dataType VARIABLE_NAME = value;

final returnType methodName() {
    // Method implementation
}

final class ClassName {
    // Class implementation
}

Example
    
final double PI = 3.14;
final class Circle {
    final void calculateArea() {
        // Area calculation implementation
    }
}
static

The static keyword is used to define a variable, method, or nested class that belongs to the class itself, rather than an instance of the class.

Syntax
    
static dataType VARIABLE_NAME = value;
static returnType methodName() {
    // Method implementation
}
static class NestedClassName {
    // Class implementation
}

Example
    
static int counter = 0;
static void incrementCounter() {
    counter++;
}
static class Utility {
    // Utility class implementation
}

abstract

The abstract keyword is used to declare an abstract class or method. An abstract class cannot be instantiated, and an abstract method must be implemented by a subclass.

Syntax
    
abstract class ClassName {
    // Class implementation
}

abstract returnType methodName();

abstract class AbstractClass {
    abstract void abstractMethod();
    void concreteMethod() {
        // Concrete method implementation
    }
}

Example
    
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        // Circle drawing implementation
    }
}

this

The this keyword is a reference to the current instance of a class. It is used to access instance variables, invoke constructors, or differentiate between instance variables and local variables.

Syntax
this.variableName;

this(argumentList);

this();

Example
    
class Person {
    String name;

    Person(String name) {
        this.name = name;
    }

    void displayName() {
        System.out.println("Name: " + this.name);
    }
}

super

The super keyword is a reference to the superclass of a class. It is used to access superclass members, invoke superclass constructors, or differentiate between superclass members and subclass members.

Syntax
super.variableName;
super(argumentList);
super();
Example
    
class Animal {
    String type;

    Animal(String type) {
        this.type = type;
    }

    void displayType() {
        System.out.println("Type: " + this.type);
    }
}

class Dog extends Animal {
    Dog() {
        super("Dog");
    }
}

instanceof

The instanceof keyword is used to check if an object belongs to a specific class or implements a specific interface.

Syntax
object instanceof className;
Example
    
class Circle {
    // Circle class implementation
}
Circle circle = new Circle();
boolean isCircle = circle instanceof Circle;