Last active
August 29, 2015 14:04
-
-
Save kenorb/9aac5bd0d6c4695e8954 to your computer and use it in GitHub Desktop.
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
| /* | |
| * Imple.java | |
| * | |
| * - Interface can have variables which are public, static and final. | |
| * | |
| * Usage: | |
| * javac Imple.java && java MainClass | |
| */ | |
| interface A { | |
| int x = 1000; | |
| void getA(); | |
| } | |
| interface B { | |
| int x = 2000; | |
| void getB(); | |
| } | |
| interface C extends A, B { | |
| void getC(); | |
| } | |
| class Imple implements C { // The same as implements A, B | |
| // class Imple implements A, B { | |
| // class Imple implements A, B, C { | |
| Imple() { | |
| // System.out.println(x); // error: reference to x is ambiguous, both variable x in A and variable x in B match. | |
| // A.x = 3000; // error: cannot assign a value to final variable x | |
| System.out.println(A.x); | |
| System.out.println(B.x); | |
| } | |
| public void getA() { | |
| System.out.println("from getA()"); | |
| } | |
| public void getB() { | |
| System.out.println("from getB()"); | |
| } | |
| public void getC() { | |
| System.out.println("from getC()"); | |
| } | |
| } | |
| class MainClass { | |
| public static void main(String[] args) { | |
| // new Imple(); | |
| Imple i1 = new Imple(); | |
| i1.getA(); | |
| i1.getB(); | |
| i1.getC(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment