Home »
Java programming language
Is it possible to use abstract and final both with a method in Java?
Here, we are going to learn that can you use abstract and final both with a method in Java? Is it possible to use abstract and final both with a method in Java?
Submitted by Preeti Jain, on September 17, 2019
No, it is not possible to use abstract and final both with a method simultaneously.
- We can use abstract only with a method without final and it is valid and the code will execute without error.
- Similarly, we can use final only with a method without abstract and it is valid and the code will execute without error.
- As we know that, we can't use abstract and final both with a method simultaneously because there are two things to discuss:
- First, if we declare abstract with method then the abstract method need to override (i.e. abstract method must be overridden in its implementation class).
- Second, if we declare final with method then the final method is final and we can't override the method (i.e. final method must not be overridden in its implementation class).
-
Few points need to remember about abstract and final method:
- What will happen, if we use abstract only with a method?
- What will happen, if we use final only with a method?
- What will happen, if we use abstract and final both with a method?
We will see each of the above cases one by one with the help of an example...
1) Using "abstract" keyword with a method in Java
Example:
// Java program to demonstrate the example of
// using "abstract" keyword with a method
abstract class AbstractMethodClass {
// Abstract Method Declaration
abstract void display();
}
public class Main extends AbstractMethodClass {
// override display() of AbstractMethodClass
public void display() {
System.out.print("abstract specifiers are allowed" + " ");
System.out.print("for Methods");
}
public static void main(String[] args) {
// class instantiation
Main m = new Main();
// calling display() of Main class
m.display();
}
}
Output
abstract specifiers are allowed for Methods
Conclusion: We can use only abstract without final for the methods.
2) Using "final" keyword with a method in Java
Example:
// Java program to demonstrate the example of
// using "final" keyword with a method
class FinalMethodClass {
// Final Method Definition
final void display() {
System.out.print("final specifier is allowed" + " ");
System.out.print("for Methods");
}
}
public class Main extends FinalMethodClass {
// show() method definition
public void show() {
System.out.print("final method is not overridable");
}
public static void main(String[] args) {
// class instantiation
Main m = new Main();
FinalMethodClass fmc = new FinalMethodClass();
// calling display() of FinalMethodClass
fmc.display();
System.out.println();
// calling display() of Main
m.show();
}
}
Output
final specifier is allowed for Methods
final method is not overridable
Conclusion: We can use only final without abstract for the methods.
3) Using "abstract" and "final" keyword both with a method in Java
Example:
// Java program to demonstrate the example of
// using "abstract" and "final" keyword both
// with a method
class AbstractFinalMethodClass {
// Abstract Method Declaration
abstract void display();
// Final Method Definition
final void show() {
System.out.print("final method is not overridable");
}
}
public class Main extends AbstractFinalMethodClass {
// override display() of AbstractFinalMethodClass
public void display() {
System.out.println("abstract method is overridable");
}
public static void main(String[] args) {
// Main class object instantiation
Main m = new Main();
// AbstractFinalMethodClass object instantiation
AbstractFinalMethodClass afmc = new AbstractFinalMethodClass();
// calling display() of Main class
m.display();
// calling show() of AbstractFinalMethodClass
afmc.show();
}
}
Output
/Main.java:5: error: AbstractFinalMethodClass is not abstract and
does not override abstract method display() in AbstractFinalMethodClass
class AbstractFinalMethodClass {
^
1 error
Conclusion: We cannot use both abstract and final with the methods because the abstract method overrides in its implementation class but the final method cannot override in its implementation class.