Skip to content

Commit 5112423

Browse files
committed
feat(interfaces): add DefaultMethodExample showcasing abstract, default, and static methods in interfaces
What - Introduced `Vehicle` interface with: - `start()` → abstract method requiring implementation. - `fuel()` → default method providing a base implementation. - `service()` → static utility method accessible via interface name. - Added `Car` and `Bike` classes: - Both override `start()` with class-specific logic. - `Car` reuses default `fuel()` method. - `Bike` overrides `fuel()` to provide custom behavior. - Added `DefaultMethodExample` main driver class to demonstrate usage. Why - Default methods (introduced in Java 8) allow adding new functionality to interfaces without breaking existing implementations. - Static methods in interfaces provide utility-like behavior tied to the interface itself. - Example demonstrates how to: 1. Implement abstract methods in classes. 2. Use or override default methods flexibly. 3. Call static methods directly from the interface. Logic 1. **Interface structure**: - `start()` = abstract, must be implemented in all classes. - `fuel()` = default method, can be inherited as-is or overridden. - `service()` = static, called via `Vehicle.service()`. 2. **Class Car**: - Implements `start()` with ignition logic. - Inherits default `fuel()` behavior from `Vehicle`. 3. **Class Bike**: - Implements `start()` with self-start logic. - Overrides `fuel()` to define petrol-specific behavior. 4. **Main flow**: - Demonstrates polymorphism (`Vehicle` reference calling concrete methods). - Shows default method reuse vs. override. - Calls static method using interface name. Real-life applications - API evolution: Adding new methods to interfaces (e.g., Java Collections API methods like `forEach`, `removeIf`). - Reducing code duplication: Shared logic (e.g., logging, resource checks) can be defined once in a default method. - Providing common utilities: Static methods inside interfaces (e.g., `Comparator.comparing()`). Notes - Default methods resolve backward compatibility problems in evolving APIs. - If two interfaces provide the same default method, implementing classes must override to resolve ambiguity. - Static methods in interfaces are **not inherited** and must be called with the interface name. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 1627b97 commit 5112423

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Interface with the default method.
2+
interface Vehicle {
3+
void start();
4+
// abstract method
5+
6+
// Default method with implementation
7+
default void fuel() {
8+
System.out.println("Vehicle needs fuel");
9+
}
10+
11+
// Static method also allowed in interface (Java 8+)
12+
static void service() {
13+
System.out.println("Vehicle service check");
14+
}
15+
}
16+
17+
class Car implements Vehicle {
18+
@Override
19+
public void start() {
20+
System.out.println("Car starts with key ignition");
21+
}
22+
}
23+
24+
class Bike implements Vehicle {
25+
@Override
26+
public void start() {
27+
System.out.println("Bike starts with self start button");
28+
}
29+
30+
// Overriding the default method (optional)
31+
@Override
32+
public void fuel() {
33+
System.out.println("Bike runs on petrol (default overridden)");
34+
}
35+
}
36+
37+
public class DefaultMethodExample {
38+
public static void main(String[] args) {
39+
Vehicle car = new Car();
40+
car.start(); // Calls abstract method from Car
41+
car.fuel(); // Calls default method from Vehicle
42+
43+
Vehicle bike = new Bike();
44+
bike.start(); // Calls abstract method from Bike
45+
bike.fuel(); // Calls overridden version in Bike
46+
47+
// Static method called from the interface itself
48+
Vehicle.service();
49+
}
50+
}
51+
52+
/*
53+
1. Abstract Method:
54+
• void start(); → must be implemented by every class.
55+
56+
2. Default Method:
57+
• default void fuel() → already has a body.
58+
• Implementing classes can either use it directly OR override it.
59+
60+
3. Static Method:
61+
• static void service() → called using the interface name (Vehicle.service()), not on objects.
62+
63+
4. Classes Car & Bike:
64+
• Both override start() (abstract method).
65+
• Car uses the default fuel() directly.
66+
• Bike overrides fuel() to provide its own behavior.
67+
*/

0 commit comments

Comments
 (0)