No, it is not possible to use abstract and final both with a method simultaneously.
不,不可能同时使用abstract和final两种方法。
We can use abstract only with a method without final and it is valid and the code will execute without error.
我们只能将抽象与没有final的方法一起使用,并且它是有效的,并且代码将正确执行。
Similarly, we can use final only with a method without abstract and it is valid and the code will execute without error.
同样,我们只能将final与没有抽象的方法一起使用,并且它是有效的,并且代码将正确执行。
As we know that, we can't use abstract and final both with a method simultaneously because there are two things to discuss:
众所周知,我们不能同时使用abstract和final两种方法,因为有两件事需要讨论:
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).
首先,如果我们用方法声明了abstract,则需要重写abstract方法(即,必须在其实现类中重写abstract方法)。
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).
其次,如果我们用方法声明final,那么final方法就是final并且我们不能覆盖该方法(即,不得在其实现类中覆盖final方法)。
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)在Java方法中使用“抽象”关键字 (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.
结论:我们只能使用摘要而不使用final作为方法。
2)在Java方法中使用“ final”关键字 (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.
结论:我们只能使用final而不使用abstract作为方法。
3)在Java方法中同时使用“ abstract”和“ final”关键字 (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.
结论:我们不能同时使用abstract和final方法,因为abstract方法在其实现类中被覆盖,而final方法不能在其实现类中被覆盖。