final keyword
In Java, the final keyword is used to declare constants variable, final
class and methods
Use of final Keyword
Prevent Changes in value of variable (e.g., final double PI = 3.14;)
Protect methods from being overridden
Secure classes from being extended
1. final with Variables
A variable marked as final cannot be reassigned after it is initialized.
final int x = 10;
x = 20; // Error: cannot assign a value to final variable 'x'
2. final with Methods
A method marked as final cannot be overridden by subclasses.
class Parent
{
final void display()
{
System.out.println("Parent Class");
}
}
class Child extends Parent
{
void display() // Error: cannot override final method
{
System.out.println("Child Class");
}
}
3. final with Classes
A class marked as final cannot be subclassed (i.e., extended).
final class Animal
{
}
class Dog extends Animal // Error: cannot subclass final class
{
}
super keyword
Super keyword is used for
To Call superclass constructor Syntax:- super();
To Call superclass method Syntax:- super.method();
To Access superclass variable Syntax:- super.variable;
1. super to Call Parent Class Constructor
You can use super() inside a subclass constructor to call a constructor
from the parent class.
class Animal
{
Animal()
{
System.out.println("Animal constructor");
}
}
class Dog extends Animal
{
Dog()
{
super(); // Calls Animal() constructor
System.out.println("Dog constructor");
}
}
Output:
Animal constructor
Dog constructor
2. super to Access Parent Class Methods
If a method in the subclass overrides a method from the parent class, you
can still call the parent’s version using super.
class Animal
{
void sound()
{
System.out.println("Animal makes sound");
}
}
class Dog extends Animal
{
void sound()
{
super.sound(); // Call parent method
System.out.println("Dog barks");
}
}
Output:
Animal makes sound
Dog barks
3. super to Access Parent Class Variables
If the subclass has a field with the same name as the parent class, you
can use super.variableName to refer to the parent’s version.
class Animal
{
String type = "Generic Animal";
}
class Dog extends Animal
{
String type = "Dog";
void printType()
{
System.out.println(super.type); // Output: Generic Animal
System.out.println(this.type); // Output: Dog
}
}
Abstract keyword
The abstract keyword in Java is used with classes and methods to
define a blueprint that must be implemented by subclasses. It plays a
key role in abstraction
abstract Class
An abstract class cannot be instantiated that mean we can’t create
object of a class
but it can have both abstract methods (without a body) and normal
methods (with a body).
abstract class Animal
{
abstract void makeSound(); // abstract method
void eat() // normal method
{
System.out.println("Animal eats food");
}
}
Animal a = new Animal(); // Error: Cannot create object the type Animal
abstract Method
An abstract method does not have a body only method declaration.
The body of function present in Subclasses
abstract class Animal
{
abstract void makeSound(); // No method body
}
class Dog extends Animal
{
void makeSound()
{
System.out.println("Dog barks");
}
}