Java 抽象类可以有 final 方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1299398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can an abstract class have a final method?
提问by keyur
Can an abstract class have a final method in Java?
Java中抽象类可以有final方法吗?
回答by pgras
Yes, those methods cannot be overriden in subclasses. An example of that is the template method pattern...
是的,这些方法不能在子类中被覆盖。一个例子是模板方法模式......
回答by Adamski
Yes it can ... need more characters
是的,它可以……需要更多字符
回答by omerkudat
Of course, it means you can subclass it, but you cannot override that particular method.
当然,这意味着您可以对它进行子类化,但不能覆盖该特定方法。
回答by jjujuma
Yes. The abstract
modifier makes it possible to omit some of the implementation of a class (i.e. have some abstract
methods) but does not impose any restrictions on you.
是的。该abstract
修改使得有可能省略一些类(即有一定的实现abstract
方法),但你不会强加任何限制。
回答by Andreas Dolk
Yes.
是的。
Hint: just fire up your favorite IDE (eclipse, netbeans, etc) and try it out. It will complain if it does not work.
提示:只需启动您最喜欢的 IDE(eclipse、netbeans 等)并尝试一下。如果它不起作用,它会抱怨。
回答by Bill the Lizard
Sure. Take a look at the Template method patternfor an example.
当然。以模板方法模式为例。
abstract class Game
{
protected int playersCount;
abstract void initializeGame();
abstract void makePlay(int player);
abstract boolean endOfGame();
abstract void printWinner();
/* A template method : */
final void playOneGame(int playersCount) {
this.playersCount = playersCount;
initializeGame();
int j = 0;
while (!endOfGame()) {
makePlay(j);
j = (j + 1) % playersCount;
}
printWinner();
}
}
Classes that extend Game
would still need to implement all abstract methods, but they'd be unable to extend playOneGame
because it is declared final.
扩展的类Game
仍然需要实现所有抽象方法,但它们无法扩展,playOneGame
因为它被声明为 final。
An abstract class can also have methods that are neither abstract nor final, just regular methods. These methods must be implemented in the abstract class, but it's up to the implementer to decide whether extending classes need to override them or not.
抽象类也可以有既不是抽象也不是最终的方法,只是常规方法。这些方法必须在抽象类中实现,但由实现者决定扩展类是否需要覆盖它们。
回答by Mnementh
Yes, it can. But the final method cannot be abstract itself (other non-final methods in the same class can be).
是的,它可以。但是final方法本身不能是抽象的(同一个类中的其他非final方法可以)。
回答by Bombe
Yes.
是的。
回答by Purushotham mekalacheruvu
In Abstract Class methods may be defined or not. If we extend the abstract class then only it has meaning, so what ever methods we declare or defined in Abstract call it will over ride in subclass. So we can declare a method as final in Abstract class, and it will be over ridden in subclass.
在抽象类中,方法可以定义也可以不定义。如果我们扩展抽象类,那么只有它才有意义,所以我们在抽象中声明或定义的任何方法调用它都会在子类中被覆盖。所以我们可以在 Abstract 类中将一个方法声明为 final,它会在子类中被覆盖。
回答by sian
Yes, there may be "final" methods in "abstract" class. But, any "abstract" method in the class can't be declared final. It will give "illegal combination of modifiers: abstract and final"error.
是的,“抽象”类中可能有“最终”方法。但是,类中的任何“抽象”方法都不能声明为 final。它会给出“修饰符的非法组合:抽象和最终”错误。
public abstract final void show();
illegal combination of modifiers: abstract and final
Here is the working example of the implementation.
这是实现的工作示例。
abstract class Sian //ABSTRACT CLASS
{
public final void show() // FINAL METHOD
{
System.out.println("Yes");
}
public void display()
{
System.out.println("Overriding");
}
public abstract void success();
}
class Ideone extends Sian //INHERTING ABSTRACT CLASS
{
public void display()
{
System.out.println("Overridden");
}
public void success() //OVERRIDING THE ABSTRACT METHOD
{
System.out.println("Success overriding");
}
public static void main (String[] args) throws java.lang.Exception
{
Ideone id = new Ideone(); //OBJECT OF SUBCLASS
id.show(); //CALLING FINAL METHOD
id.display(); //OVERRIDDEN METHODS
id.success();
}
}
OUTPUT:-
输出:-
Yes
Overridden
Success overriding
Here is the ideone link:- http://ideone.com/G1UBR5
这是 ideone 链接:- http://ideone.com/G1UBR5