关于嵌套类中的抽象类的问题。

huangapple go评论66阅读模式
英文:

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).

huangapple
  • 本文由 发表于 2020年10月1日 05:31:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64146035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定