英文:
Question about Abstract Classes when classes are nested
问题
如果ClassB
被声明为abstract
因为它包含抽象方法,那么ClassA
是否也必须被声明为Abstract
?
英文:
So let's say ClassB
is a class defined inside ClassA
(Nested Classes), my question is, if ClassB
is declared abstract
as it contains abstract Methods, would ClassA also have to be declared Abstract
?
答案1
得分: 1
No. 每个类的方法属于它自己。一个类不拥有其他嵌套在其中的类(或接口!)的方法,并且包含嵌套抽象类或嵌套接口不要求容器类是抽象的。
英文:
No. The methods of each class are its own. A class does not own the methods of other classes (or interfaces!) nested within, and containing a nested abstract class or a nested interface does not require the container class to be abstract.
答案2
得分: 1
不,它并不会。无论外部类还是父类是否为abstract
,在Java中都没有强制要求类是abstract
的规定。
一个例子:
class classA {
abstract class classB { // 也可以是静态的
abstract void foo();
}
}
另一方面,方法略有不同:abstract
方法必须只能放在abstract
类或接口(隐式抽象)中。
英文:
Nope, it doesn't. It doesn't matter whether the outer or parent class is abstract
, there is no rule in Java enforcing a class being abstract
.
An example:
class classA {
abstract class classB { // can also be static
abstract void foo();
}
}
On the other hand, it's a bit different with the methods: abstract
methods must be placed only inside the abstract
classes or interfaces (implicitely abstract).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论