C# 为什么 Java 类不能既是抽象类又是最终类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19110342/
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
Why can't a Java class be both abstract and final
提问by PC.
Suppose I've a utility class which contains only static methods and variables. e.g:
假设我有一个仅包含静态方法和变量的实用程序类。例如:
public abstract final class StringUtils
{
public static final String NEW_LINE = System.getProperty("line.separator");
public static boolean isNotNullOrSpace(final String string)
{
return !(string == null || string.length() < 1 || string.trim().length() < 1);
}
}
In this scenario, it makes sense to make the class both abstract and final. Abstract because making an object of this class will be of no use as all methods are accessible statically. Final because the derived class cannot inherit anything from this class as it does not have any non-static member.
在这种情况下,使类既抽象又最终是有意义的。抽象是因为创建此类的对象将没有用,因为所有方法都可以静态访问。Final 因为派生类不能从这个类继承任何东西,因为它没有任何非静态成员。
C# allows staticmodifier for such classes. Why doesn't Java support this?
C# 允许此类类的静态修饰符。为什么Java不支持这个?
采纳答案by ssantos
A final
class can't be extended, an abstract
class needsto be extended in order to be instantiated. Therefore, a final abstract
class would be a logical contradiction.
一个final
类不能被扩展,一个abstract
类需要被扩展才能被实例化。因此,一个final abstract
类将是一个逻辑矛盾。
If your class just have static
methods, maybe you should just hide
its constructor, by defining it as private
.-
如果你的类只有static
方法,也许你应该只是hide
它的构造函数,通过将它定义为private
.-
private StringUtils() {
}
回答by user1515520
There is no reason except subjectivity but you can accomplish the same objective by making your class abstract and make all your methods final. Like:
除了主观性之外没有其他原因,但是您可以通过使类抽象并使所有方法成为最终方法来实现相同的目标。喜欢:
public abstract class MyClass {
…
public final static void myMethod1() {
…
}
public final static void myMethod2() {
…
}
}
The compiler will check this and give an error if you try to instantiate an object of MyClass, and it will also not be possible to override the final methods when any subclass extends MyClass
如果您尝试实例化 MyClass 的对象,编译器将对此进行检查并给出错误,并且当任何子类扩展 MyClass 时,也无法覆盖最终方法
回答by Joe Lee-Moyet
It is not possible because the Java language specification states that:
这是不可能的,因为 Java 语言规范指出:
It is a compile-time error to declare an abstract class type such that it is not possible to create a subclass that implements all of its abstract methods [1]
声明抽象类类型是编译时错误,因此无法创建实现其所有抽象方法的子类[1]
Other than this, there is no inherent reason why an abstract final class would be illogical - the word abstractis generally used in contrast to concreteto mean that no direct instances of a type may exist.
除此之外,没有任何内在原因为什么抽象最终类会不合逻辑 -抽象一词通常与具体相反,表示可能不存在类型的直接实例。
This is the same reason why abstract methods cannot have access modifier private.
这与抽象方法不能拥有私有访问修饰符的原因相同。
回答by Sreedhar GS
A method can never, ever, ever be marked as both abstract and final, or both abstract and private. Think about it—abstract methods must be implemented (which essentially means overridden by a subclass) whereas final and private methods cannot ever be overridden by a subclass. Or to phrase it another way, an abstract designation means the superclass doesn't know anything about how the subclasses should behave in that method, whereas a final designation means the superclass knows everything about how all subclasses (however far down the inheritance tree they may be) should behave in that method. The abstract and final modifiers are virtually opposites. Because private methods cannot even be seen by a subclass (let alone inherited), they too cannot be overridden, so they too cannot be marked abstract.
一个方法永远不能被标记为抽象和最终,或抽象和私有。想一想——抽象方法必须被实现(这本质上意味着被子类覆盖),而最终和私有方法永远不能被子类覆盖。或者换一种说法,抽象指定意味着超类不知道子类在该方法中应该如何表现,而最终指定意味着超类知道所有子类的所有信息(无论它们在继承树中有多远) be) 应该在该方法中表现。abstract 和 final 修饰符实际上是对立的。因为私有方法甚至不能被子类看到(更不用说继承了),它们也不能被覆盖,所以它们也不能被标记为抽象的。
回答by Ankur08
In Java an instance of an abstract class cannot be created, we can only have references of abstract class type. So there if we make abstract class final then we wont be able to extend it. abstract and final are the mutual exclusive concept. that's why Java compiler throws a compile time error when you try to make an abstract class final in Java
在 Java 中不能创建抽象类的实例,我们只能拥有抽象类类型的引用。因此,如果我们将抽象类设为 final,那么我们将无法扩展它。abstract 和 final 是互斥的概念。这就是为什么当您尝试在 Java 中创建抽象类 final 时,Java 编译器会抛出编译时错误的原因
回答by Kumar Anil Chaurasiya
Due to some certain reasons we can not final use final with abstract class. 1. If we define abstract final then we can't extend it. 2. If we are defining abstract class as final the it will give the compile time error..
由于某些原因,我们不能在抽象类中最终使用 final。1.如果我们定义abstract final,那么我们就不能扩展它。2.如果我们将抽象类定义为final,它将给出编译时错误..