Created
January 14, 2019 15:31
-
-
Save theArjun/389fed91d9ea48352aed91a62ed692b1 to your computer and use it in GitHub Desktop.
Nested Interface Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Animal{ | |
| // Creating the interface inside class; nested interface. | |
| interface Activity{ | |
| public default void move(){ | |
| System.out.println("Animal is moving."); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // To get access to Nested Interface : we write OuterClassName.NestedClasName | |
| class Dog implements Animal.Activity{ | |
| public void move(){ | |
| System.out.println("Dog is moving."); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NestedInterfaceDemo{ | |
| public static void main(String[] args){ | |
| Dog dogObject = new Dog(); | |
| dogObject.move(); | |
| // This is how, we refer to subclass object from nested interface referrer variable. | |
| Animal.Activity nestedReferrerOne = new Dog(); | |
| nestedReferrerOne.move(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment