-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntefaceRules.java
59 lines (48 loc) · 1.53 KB
/
IntefaceRules.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package concept.examples.oops.interfaces;
public class IntefaceRules {
}
interface ExampleInterface1 {
// By default - public static final. No other modifier allowed
// value1,value2,value3,value4 all are - public static final
int value1 = 10;
public int value2 = 15;
public static int value3 = 20;
public static final int value4 = 25;
// private int value5 = 10;//COMPILER ERROR
// By default - public abstract. No other modifier allowed
void method1();// method1 is public and abstract
// private void method6();//COMPILER ERROR!
//Interface can have a default definition of method.
//NEW FEATURE
default void method5() {
System.out.println("Method5");
}
}
interface ExampleInterface2 {
void method2();
}
// An interface can extend another interface
// Class implementing SubInterface1 should
// implement method3 and method1(from ExampleInterface1)
interface SubInterface1 extends ExampleInterface1 {
void method3();
}
/*
* //COMPILE ERROR IF UnCommented //Interface cannot extend a Class interface
* SubInterface2 extends InterfaceRules{ void method3(); }
*/
/* A Class can implement multiple interfaces */
class SampleImpl implements ExampleInterface1, ExampleInterface2 {
/*
* A class should implement all the methods in an interface. If either of
* method1 or method2 is commented, it would result in compilation error.
*/
@Override
public void method2() {
System.out.println("Sample Implementation for Method2");
}
@Override
public void method1() {
System.out.println("Sample Implementation for Method1");
}
}