简体   繁体   中英

Multiple inherited classes, do they have to be in separate files?

I'm implementing a system where I have an interface named 'MyMethod' (the name is arbitrary), and lots of small classes are implementing this method (specifically to override it's 'call' method), and I'm using polymorphism to create instances of these into a list.

The thing is, as I have lots of these small classes implementing the method, it was easier to put these classes in the same java file (MyMethod.java), like so:

public interface MyMethod {
    public String call(foo param1, bar param2) throws SQLException, IOException;
}

class FooMethod1 implements MyMethod {
    @Override
    public String call(foo param1, bar param2) throws SQLException, IOException {
        //Do Something
    }
}

class FooMethod2 implements MyMethod {
    @Override
    public String call(foo param1, bar param2) throws SQLException, IOException {
        //Do Something Different
    }
}

However, putting these classes in the same file, although neater, seems like I'm going against the way things should be done in Java.

Is it okay to have these classes in the same file? or should I move them each to a separate file?

Thanks

Yes, it is ok to put several package-private classes in the same file.

The relevant section of the JLS is §7.3

CompilationUnit:
    PackageDeclaration[opt] ImportDeclarations[opt] TypeDeclarations[opt]

TypeDeclarations:
    TypeDeclaration
    TypeDeclarations TypeDeclaration

As you can see, a compilation unit may have several type declarations (or zero). Another relevant section of the JLS is §7.6 . It explains the correlation between those type declarations that are accessed from outside of a compilation unit, and the compilation unit's file name:

If and only if packages are stored in a file system ( §7.2 ), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

  • The type is referred to by code in other compilation units of the package in which the type is declared.

  • The type is declared public (and therefore is potentially accessible from code in other packages).

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler to find a named class within a package. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.

Clearly, your approach isn't against Java and perfectly OK. However, beware that your MyMethod implementations may not be available outside of the MyMethod.java compilation unit, depending on the compiler implementation. Although, neither javac nor the Eclipse compiler seem to have issues with this...

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