In Java, is it possible to have Interface in Inner class?

Recommended Answers

All 2 Replies

Your title and question are inconsistent!
Is it interfaces in inner classes, inner classes in interfaces?

Interfaces in inner classes:

Yes, sometimes.
Because interfaces are by definition static, they can only be declared in a static inner class.
Thus:

class Testing {
    static class Inner {
        interface InnerInterface {
        }
    }
}

is legal, but

class Testing {
    class Inner {
        interface InnerInterface {
        }
    }
}

is not.

Inner classes in interfaces:

Yes:

interface I {
    class C{}
}

is legal.

ps: You could have got a quicker answer by trying that code yourself!

Wow. When did we last see such a useless pile of nonsense. Almost every line contains an error. There's no way you could turn that into valid Java code.
And the link is to a useless tutorial that doesn't actually explain anything and doesn't even use normal Java coding standards.

If you are looking for a tutorial there's only one that is up-to-date, comprehensive, and correct, and that's Oracle's own...
https://docs.oracle.com/javase/tutorial/

commented: Maybe they are trying to make the Kessel run in less than 12 parsecs? +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.