0% found this document useful (0 votes)
4 views4 pages

Key Java Keywords Explained: this, super, abstract, final, static

Java

Uploaded by

dannyhumble361
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Key Java Keywords Explained: this, super, abstract, final, static

Java

Uploaded by

dannyhumble361
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

In Java programming, certain keywords are used to define the behaviour, inheritance, structure, and

memory management of a program. Understanding these keywords is essential for writing robust,
maintainable, and efficient code. This essay discusses the following Java keywords and concepts: this,
super, abstract class, final, and static. Each concept will be explained with definitions, examples, and
relevance to object-oriented programming (OOP).

1)The this Keyword

The ‘this’ keyword in Java is a reference variable that represents the current object, the object whose
method or constructor is being invoked. It is used mainly to eliminate confusion between instance
variables and parameters with similar names, and to invoke current class constructors and methods.

Uses of this:
 To refer to the current object’s instance variables
 To call another constructor in the same class (this())
 To invoke current object methods
 To return the current object
Example
class Student {
String name;
int age;
Student(String name, int age) {
[Link] = name;
[Link] = age;
}
void display() {
[Link]("Name: " + [Link] + ", Age: " + [Link]);
}
}

Explanation
In the constructor, the [Link] refers to the instance variable, while name refers to the constructor
parameter.

2)The super Keyword


The ‘super’ keyword is used to refer to the immediate parent class object. It allows access to parent-class
methods and constructors, especially when method overriding is involved.

Uses of super:

 Access parent class variables


 Invoke parent class methods
 Call parent class constructor (super())
Example
class Animal {
String type = "Animal";
Animal() {
[Link]("Animal constructor called.");
}
void sound() {
[Link]("Animals make sound.");
}
}

class Dog extends Animal {


String type = "Dog";

Dog() {
super();
}
void sound() {
[Link]();
[Link]("Dogs bark.");
}

void printType() {
[Link]([Link]);
[Link]([Link]);
}
}

Explanation
[Link]() calls the parent method, while [Link] accesses the parent variable. This demonstrates
inheritance and method overriding.

3)Abstract Class
An abstract class is a class that cannot be instantiated. It may contain both abstract methods (without
bodies) and concrete methods (with bodies). Abstract classes provide a common template for subclasses.
Characteristics:

 Declared using the keyword abstract.


 Can contain abstract and non-abstract methods.
 Must be inherited; subclasses MUST implement abstract methods unless they are also abstract.

Example
abstract class Shape {
abstract void draw();
void display() {
[Link]("This is a shape.");
}
}

class Circle extends Shape {


void draw() {
[Link]("Drawing a circle...");
}
}

Explanation
The Shape class provides a blueprint, while Circle implements the abstract method.

4)The final Keyword


The ‘final’ keyword is used in Java to restrict modification. It can be applied to variables, methods, and
classes.
Uses:

 Final variable → value cannot be changed (acts like a constant)


 Final method → cannot be overridden
 Final class → cannot be inherited

Examples
Final variable
final int AGE = 20;
// AGE = 30; // Error: cannot change final variable
Final method
class Parent {
final void show() {
[Link]("Parent show method.");
}
}

class Child extends Parent {


// void show() {} // Error: cannot override final method
}
Final class
final class Vehicle {}
// class Car extends Vehicle {} // Error: final classes cannot be inherited
Explanation
The final keyword is important in security-critical or stable systems where certain values or behaviors
must not be modified.

5)The static Keyword


The ‘static’ keyword is used for elements that belong to the class itself, not to individual objects. Static
members are shared across all objects of a class.
Static can be applied to:

 Variables (class-level shared variables)


 Methods (can be called without creating an object)
 Blocks (executed when class loads)
 Inner classes (static nested classes)

Example
class Counter {
static int count = 0;

Counter() {
count++;
}
static void displayCount() {
[Link]("Objects created: " + count);
}
}

public class Test {


public static void main(String[] args) {
new Counter();
new Counter();
[Link]();
}
}
Explanation
The variable count increments regardless of which object is created since it belongs to the class.
Conclusion
The keywords this, super, abstract, final, and static are foundational to Java’s object-oriented
programming model. Each keyword provides structure, clarity, and control over class behaviour,
inheritance, memory management, and method implementation. Understanding these concepts not
only improves code quality but also enhances maintainability and reliability in large-scale software
development.
References

 Schildt, H. (2018). Java: The Complete Reference. McGraw-Hill Education.


 Oracle Java Documentation: The Java® Language Specification.
 Oracle Java Tutorials: [Link]/javase/tutorial

You might also like