Difference between Final and Abstract in Java
In Java, the keywords final and abstract are used to define different characteristics and behaviours of classes, methods and variables. While both keywords play important roles in Java programming, they serve distinct purposes.
1. Final:
The final keyword is used to specify that an entity (class, method, or variable) cannot be changed or overridden once it has been defined.
Key aspects of final:
Final Class: When a class is marked as final it means that the class cannot be subclassed. In other words you cannot extend a final class to create a new class. It is typically done when you want to prevent any further modifications or extensions to a class ensuring its integrity and stability.
Final Method: When a method is marked as final it cannot be overridden by any subclass. It ensures that the behavior of the method remains consistent across all subclasses preventing any modifications to the method implementation.
Final Variable: When a variable is declared as final and its value cannot be changed once it has been assigned. A final variable is essentially a constant and you can only assign an value to it once during initialization. It is useful when you want to create variables whose values should not be modified later in the program.
Implementation:
FileName: FinalExample.java
import java.util.*; final class FinalClass { // Final variable final int finalVariable=10; // Final method final void finalMethod() { System.out.println("This is a final method."); } } class FinalExample { public static void main(String[] args) { FinalClass finalObj=new FinalClass(); // Attempting to modify the final variable(will result in a compilation error) finalObj.finalMethod(); } }
Output:
This is a final method.
Explanation:
The FinalClass in the code is declared as final, which means it cannot be further subclassed. The finalVariable within the class is assigned a value of 10 and marked as final, ensuring its value cannot be changed. The finalMethod() is also marked as final, preventing any subclasses from overriding its implementation. Any attempt to create a subclass of FinalClass will result in a compilation error due to its final status. In the Main class, an object of FinalClass is created, but any attempt to modify the finalVariable will lead to a compilation error. However, the finalMethod() can be called on the finalObj without any issues as it can still be invoked.
2. Abstract:
The abstract keyword is used to define abstract classes and methods in Java. An abstract class is a class that cannot be instantiated meaning you cannot create objects of an abstract class directly.
key points about abstract:
Abstract Class: A class that has the abstract keyword in its declaration is an abstract class. It can have both abstract and non-abstract methods and acts as a model for subclasses. Although they cannot be created on their own, abstract classes can be subclassed. An abstract class's subclasses must either implement every abstract method in the superclass or else they must also be explicitly marked as abstract.
Abstract Method: An abstract method is a method that is declared without an implementation in an abstract class. It is defined using the abstract keyword and ends with a semicolon instead of a method body. Subclasses of an abstract class must provide implementations for all the abstract methods inherited from the superclass.
Implementation:
FileName: AbstractExample.java
import java.util.*; abstract class AbstractClass { // Abstract method abstract void abstractMethod(); // Non-abstract method void nonAbstractMethod() { System.out.println("This is a non-abstract method."); } } // Subclass extending the abstract class class Subclass extends AbstractClass { void abstractMethod() { System.out.println("Abstract method implementation in the subclass."); } } class AbstractExample { public static void main(String[] args) { Subclass subclassObj=new Subclass(); subclassObj.abstractMethod(); subclassObj.nonAbstractMethod(); } }
Output:
Abstract method implementation in the subclass. This is a non-abstract method.
Explanation:
The code defines an abstract class called AbstractClass using the abstract keyword. It contains an abstract method called abstractMethod() which lacks an implementation and a non-abstract method named nonAbstractMethod() that has a complete implementation. The Subclass extends the AbstractClass and provides its own implementation for the abstractMethod() by overriding it. In the Main class, an object of Subclass is created. The abstractMethod() is invoked on the subclassObj, which executes the overridden implementation in the subclass. Additionally, the nonAbstractMethod() is also called on the subclassObj, demonstrating that non-abstract methods from the abstract class can be invoked without any issues.
Some of Key Differences:
Final | Abstract |
Prevents further modification or extension | Provides a blueprint for subclasses |
Used for classes, methods and variables | Used for classes, methods. |
Final class cannot be subclassed | Abstract class cannot be instantiated directly |
Final method cannot be overridden | Abstract method does not have an implementation |
Final variable's value cannot be changed | Abstract methods must be implemented in subclass |
Final class, method, or,variable must be defined with a specific value | Abstract class provide a common interface or behaviour |
Used to enforce immutability and integrity | Used to define a contract for subclasses |
Final methods can be directly invoked | Abstract classes can contain both abstract and non-abstract methods |