-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaceDemo.java
More file actions
153 lines (110 loc) · 3.17 KB
/
InterfaceDemo.java
File metadata and controls
153 lines (110 loc) · 3.17 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// ---------- BASIC INTERFACE ----------
interface Animal {
void makeSound(); // abstract method
}
// ---------- IMPLEMENTING INTERFACE ----------
class Dog implements Animal {
public void makeSound() {
System.out.println("Dog barks");
}
}
// ---------- MULTIPLE INTERFACES ----------
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
class Duck implements Flyable, Swimmable {
public void fly() {
System.out.println("Duck can fly");
}
public void swim() {
System.out.println("Duck can swim");
}
}
// ---------- DEFAULT METHODS ----------
interface Vehicle {
void start(); // abstract method
default void fuelType() {
System.out.println("Default fuel type: Petrol");
}
}
class Car implements Vehicle {
public void start() {
System.out.println("Car starts with key");
}
}
// ---------- STATIC METHODS ----------
interface MathUtility {
static int square(int x) {
return x * x;
}
}
// ---------- PRIVATE METHODS (Java 9+) ----------
interface Greeting {
default void sayHello() {
privateHelper();
System.out.println("Hello from interface");
}
private void privateHelper() {
System.out.println("Private helper method");
}
}
// ---------- INTERFACE INHERITANCE ----------
interface ParentInterface {
void parentMethod();
}
interface ChildInterface extends ParentInterface {
void childMethod();
}
class InterfaceChild implements ChildInterface {
public void parentMethod() {
System.out.println("Parent interface method");
}
public void childMethod() {
System.out.println("Child interface method");
}
}
// ---------- FUNCTIONAL INTERFACE ----------
@FunctionalInterface
interface Calculator {
int operate(int a, int b);
}
// ---------- MAIN CLASS ----------
public class InterfaceDemo {
public static void main(String[] args) {
// Basic interface implementation
Animal d = new Dog();
d.makeSound();
System.out.println("-------------------");
// Multiple interfaces
Duck duck = new Duck();
duck.fly();
duck.swim();
System.out.println("-------------------");
// Default method
Car car = new Car();
car.start();
car.fuelType();
System.out.println("-------------------");
// Static method
int result = MathUtility.square(5);
System.out.println("Square = " + result);
System.out.println("-------------------");
// Private method via default method
Greeting g = new Greeting() {};
g.sayHello();
System.out.println("-------------------");
// Interface inheritance
InterfaceChild obj = new InterfaceChild();
obj.parentMethod();
obj.childMethod();
System.out.println("-------------------");
// Lambda expression using functional interface
Calculator add = (a, b) -> a + b;
Calculator multiply = (a, b) -> a * b;
System.out.println("Addition: " + add.operate(5, 3));
System.out.println("Multiplication: " + multiply.operate(5, 3));
}
}