Final Keyword In Java
Final Keyword In Java
The final keyword in java is used to restrict the user. The java final keyword can be used in many
context.
Final can be:
Variable
Method
class
The final keyword can be applied with the variables, a final variable that have no value it is called
blank final variable or uninitialized final variable.
It can be initialized in the constructor only. The blank final variable can be static also which will be
initialized in the static block only. We will have detailed learning of these. Let's first learn the basics
of final keyword.
Output:
Output:Compile Time Error
For example,
class OverloadingExample{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
File Name: Calculator.java
For example,
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
}
File Name: MethodOverriding.java
// Superclass Animal
class Animal {
// Method to be overridden in subclass
public void makeSound() {
System.out.println("Animal makes a sound"); // Prints a generic animal sound message
}
}
// Subclass Dog that extends Animal
class Dog extends Animal {
// Overriding the makeSound method in the superclass
@Override
public void makeSound() {
System.out.println("Dog barks"); // Prints a specific message indicating a dog barking
}
}
// Main class to test method overriding
public class MethodOverriding {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create an Animal object and assign it to a variable of type Animal
Animal myDog = new Dog(); // Create a Dog object but reference it as an Animal type
// Call makeSound method on both objects
myAnimal.makeSound(); // Calls the makeSound method of the Animal class, prints "Animal makes a sound"
myDog.makeSound(); // Calls the overridden makeSound method of the Dog class, prints "Dog barks"
} }
Output:
Disadvantages of Overriding
1. Inconsistent Behavior: Poor implementation of method overriding can result in inconsistent behavior across
subclasses, complicating code comprehension and debugging efforts.
2. Fragile Base Class Problem: Alterations in the behavior of overridden methods in the parent class can
inadvertently impact the functionality of subclasses.
3. Tight Coupling: Method overriding can create tight coupling between parent and child classes, potentially
reducing flexibility and ease of maintenance.
4. Access Control Limitations: Overridden methods must have the same or less restrictive access modifiers
than the original method, which can limit access control options.
5. Misuse of Inheritance: Method overriding can be misused, leading to overly complex inheritance hierarchies
or inappropriate parent-child relationships.
6. Runtime Performance Impact: Overriding methods can introduce slight performance overhead due to the
dynamic dispatch mechanism used to determine the correct method at runtime.
7. Method Signature Constraints: Overridden methods must maintain the same method signature as the
original method, restricting changes to parameters or return type.
8. Difficulty in Method Hiding: Overridden methods can hide static methods with the same name in the parent
class, causing confusion and potential runtime errors.
9. Limited Method Combination: Overriding methods cannot combine the behavior of multiple parent class
methods, as only a single overridden method can exist.
10. Complexity in Method Chains: Method overriding can introduce complexities in method chaining scenarios,
potentially leading to unexpected results or incorrect behavior.
In case of method overloading, parameter must be In case of method overriding, parameter must
different. be same.
Method overloading is the example of compile time Method overriding is the example of run time
polymorphism. polymorphism.
If any error occurs, can be caught at compile-time. If any error occurs, can be caught at run-time.
It is applicable to both private and final methods. It is not applicable to private and final methods.