inner class within Interface

2019-01-03 12:17发布

is that possible to create a inner class within an interface? If yes, why do we create like that? Anyways we are not going to create any interface objects?

Do they help in any Development process?

13条回答
神经病院院长
2楼-- · 2019-01-03 12:49

What @Bachi mentions is similar to traits in Scala and are actually implemented using a nested class inside an interface. This can be simulated in Java. See also java traits or mixins pattern?

查看更多
做个烂人
3楼-- · 2019-01-03 12:49

I found a use fir this type of construct.

  1. You can use this construct to defines and group all the static final constants.
  2. Since, it is an interface you can implement this on an class.

You have access to all the constants grouped; name of the class acts as a namespace in this case.

查看更多
该账号已被封号
4楼-- · 2019-01-03 12:52

Quote from the Java 7 spec:

Interfaces may contain member type declarations (§8.5).

A member type declaration in an interface is implicitly static and public. It is permitted to redundantly specify either or both of these modifiers.

It is NOT possible to declare non-static classes inside a Java interface, which makes sense to me.

查看更多
beautiful°
5楼-- · 2019-01-03 12:54

Yes, you can create both a nested class or an inner class inside a Java interface (note that contrarily to popular belief there's no such thing as an "static inner class": this simply makes no sense, there's nothing "inner" and no "outter" class when a nested class is static, so it cannot be "static inner").

Anyway, the following compiles fine:

public interface A {
    class B {
    }
}

I've seen it used to put some kind of "contract checker" directly in the interface definition (well, in the class nested in the interface, that can have static methods, contrarily to the interface itself, which can't). Looking like this if I recall correctly.

public interface A {
    static class B {
        public static boolean verifyState( A a ) {
            return (true if object implementing class A looks to be in a valid state)
        }
    }
}

Note that I'm not commenting on the usefulness of such a thing, I'm simply answering your question: it can be done and this is one kind of use I've seen made of it.

Now I won't comment on the usefulness of such a construct and from I've seen: I've seen it, but it's not a very common construct.

200KLOC codebase here where this happens exactly zero time (but then we've got a lot of other things that we consider bad practices that happen exactly zero time too that other people would find perfectly normal so...).

查看更多
甜甜的少女心
6楼-- · 2019-01-03 12:55

Maybe when you want more complex constructions like some different implementation behaviours, consider:

    public interface A {
    public void foo();
    public static class B implements A{
        @Override
        public void foo(){
            System.out.println("B foo");
        }
    }
}

This is your interface and this will be the implementee:

    public class C implements A {
    @Override
    public void foo(){ A.B b = new A.B(); b.foo(); }


    public static void main(String[] strings) {
        C c = new C();
        c.foo();
    }
}

May provide some static implementations, but won't that be confusing, I don't know.

查看更多
Luminary・发光体
7楼-- · 2019-01-03 12:56

You can also create "Helper" static classes for common functionality for the objects that implement this interface:

public interface A {
    static class Helper {
        public static void commonlyUsedMethod( A a ) {
           ...
        }
    }
}
查看更多
登录 后发表回答