![]() For example, if you have an interface with an exceedingly common name, and in a large project, it is quite possible that some other programmer has the same idea, and has an interface with the same name you had, then you can solve this potential name clash by making your interface a public static nested interface. This is the basic idea behind introducing nested interfaces and nested classes in Java. Let's take a look at the following example, and you will understand that how you can control the visibility of a nested interface when it is declared inside a class.Ī static nested interface serves a great advantage to namespace resolution. It cannot be accessed from outside the class that contains it. However, a private nested interface declared within a class can only be extended by other nested interfaces or implemented by other nested or inner classes. You can declare a nested interface public, protected, or private depending upon the level of encapsulation. As we have seen, inside an interface a nested interface is always public but this is not the case when a nested interface is declared inside a class. But visibility of a nested interface can be controlled when it is declared inside a class. One rule is common for nested interfaces that they are implicitly static no matter if they are declared within an interface or a class. Within from aaMethod Nested Interface Declaration within Class javaĭ :\JavaPrograms >java NestedInterfaceDemo * NestedInterfaceDemo.java */ interface Aĭ :\JavaPrograms >javac NestedInterfaceDemo. Given that rule, we can see that the following two nested interface declarations are exactly equivalent: Nested Interface Declaration within InterfaceĪs said earlier, when an interface is declared within another interface it is implicitly public and static therefore, adding those modifiers is considered redundant and makes no difference. Nested interface can be implemented by any class (package level, nested or inner) if the access modifiers permit visibility. ![]() Nested interface declared inside a class can accept any access modifiers.Nested interface declared inside another interface is implicitly public.Nested interfaces are implicitly static regardless of where they are declared (inside class or another interface). ![]() These rules apply depending upon where the nested interface declaration occurs inside another interface or class. To declare a nested interface there are certain rules. An interface cannot be instantiated on its own therefore, it must be implemented by a class or extended by other interface to be used. By creating an interface, we define a contract for what a class can do if it implements the interface, without saying anything about how the class will do it. Regardless of top level or nested interface, an interface declaration introduces a new hundred percent abstract reference type, whose members are classes, interfaces, constants, and abstract methods. A top level interface is an interface that is not a nested interface. Nested interface is also called inner interface.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |