简体   繁体   中英

If a method returns an anonymous class, is it returning a subclass or an inner class?

Perhaps it makes no difference, but I want to know anyways. I want to know what it means to return an anonymous class such as in the following:

public class Module extends BaseModule {

    private BaseInitializable initializeModule() {
        return new BaseInitializable() {
            public void initialize(InitializationContext context) {
                helperA.initialize();
                helperB.initialize();
                s_logger.log(LogLevel.INFO,"something");
            }

            public void shutdown(InitializationContext context) {
            }
        };
    }
}

Post 1

Post 2

Based on post 1, it seems the code is returning an anonymous inner class. But doesn't that go against the principle of an inner class in that they are only relevant to the parent class in which they reside (according to post 2)? The accepted answer in post 1 says the inner class is also sub class of the parent class. If that's true, then it makes sense to return this inner/sub class. But according to Post 2, inner classes are not subclasses. So what am I to make of this? Does this method return an instance of an inner class or a subclass?

This statement "[inner classes] are only relevant to the parent class in which they reside" is simply wrong.

The inner class has a hidden reference to the outer class, and it has permission to access the private members of the outer class. That's basically all that makes it special.

The method in your question does indeed return an instance of the anonymous inner class derived from or implementing BaseInitializable (is that an interface or class?).

Note that if BaseInitializable is a class then this anonymous inner class is also a sub-class of BaseInitializable. If BaseInitializable is an interface then technically it is not a sub-class.

The inner class, in this case, is NOT a subclass of the outer class. I think that is what Post 2 is talking about.

I want to know what it means to return an anonymous class such as in the following:

    return new BaseInitializable() {
        ...
    };

That depends on what BaseInitializable is. If it is a class , then the anonymous class is a subclass of BaseInitializable . If it is an interface , then the anonymous class is a subclass of Object and it implements BaseInitializable .

Also, remember that anonymous classes by definition are inner classes. So says JLS 15.9.5. Anonymous Class Declarations :

An anonymous class is always an inner class.

Which means, to answer your question:

Does this method return an instance of an inner class or a subclass?

Both. The anonymous class is an inner class and a subclass (of BaseInitializable or of Object ).

Or, if you don't consider classes with Object as their superclass to be subclasses:
It depends. The anonymous class is an inner class. It is also a subclass if it implements a class , not an interface .

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