简体   繁体   中英

Do I need to import into subclasses java

Suppose I have a superclass which imports java.io.Exception as an example. If I'm handling an exception in a subclass, do I need to reimport java.io.Exception into the code for the subclass?

Yes (unless the two classes are in the same file).

Imports are per source code file, not per class. They are a completely compiler-only thing, with no effect at run-time.

是,如果子类在另一个.java文件中,则为否

The import scope is per source file 1 : it adds nothing to a class/type and thus has nothing to do with inheritance or subclasses.

Typing out each fully-qualified type is too cumbersome, even for Java, so imports specify how to find/resolve unqualified types (or static methods).

The compiler will kindly let you know if an import is missing when it can't resolve the type(s) ..


1 Import statements must appear after the package name, if any, and before all types/classes declared in the file. Since there is at most one package per file, there is at most one section of imports per file and their scope encompasses the remainder of the file.

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