简体   繁体   中英

Question about Abstract Classes when classes are nested

因此,假设ClassB是在ClassA (嵌套类)中定义的类,我的问题是,如果ClassB被声明为abstract因为它包含抽象方法,那么 ClassA 是否也必须声明为Abstract

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM