Skip to content

Commit 28482ff

Browse files
committed
feat: Demonstrate multiple inheritance in Java using interfaces Walkable and Swimmable with Duck implementation
WHAT the code does: - Declares interface Walkable with abstract method walk(). - Declares interface Swimmable with abstract method swim(). - Defines class Duck: - Implements both Walkable and Swimmable. - Provides concrete implementations of walk() and swim(), printing duck-specific behavior. - In MultipleInheritanceUsingInterfaces.main(): - Creates a Duck object. - Calls walk() and swim() to demonstrate the behavior. WHY this matters: - Java does not support multiple class inheritance (a class can extend only one class), but it does support multiple interface inheritance. - This pattern allows a class to inherit multiple capabilities, promoting flexible and modular design. - Interfaces represent “abilities” or “roles” (e.g., walk, swim), while the implementing class ties them together into a concrete entity. - Encourages composition of behaviors without introducing ambiguity or the diamond problem found in multiple class inheritance. HOW it works: 1. Interfaces Walkable and Swimmable define capabilities (walking and swimming). 2. Duck implements both, making it contractually obliged to define walk() and swim(). 3. At runtime, Duck objects can be used wherever Walkable or Swimmable references are required, enabling polymorphism. 4. In main(), duck.walk() and duck.swim() trigger the Duck’s implementations. Tips and gotchas: - Always use interface names as abilities or roles (Walkable, Swimmable, Flyable) — it improves readability. - A class can implement any number of interfaces but extend only one class. - Interfaces define contracts, not state — fields should be avoided except for constants. - Use polymorphism: Duck can be referenced as Walkable w = new Duck(); w.walk(); — this limits the reference to the walk() ability only. Use-cases / analogies: - Animals in biology: ducks can both walk and swim, dolphins swim but don’t walk, humans walk but can’t naturally swim well — different classes can combine interfaces differently. - Vehicles: an AmphibiousCar could implement both Drivable and Swimmable. - Game design: characters may implement multiple roles (Attackable, Healable, Movable) simultaneously. Short key: java-interfaces multiple-inheritance duck walkable-swimmable polymorphism. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 3117171 commit 28482ff

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
/*
2-
Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when methods with the same signature exist in both the superclasses and subclass.
2+
Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one
3+
parent class.
4+
The problem occurs when methods with the same signature exist in both the superclasses and subclass.
35
On calling the method, the compiler cannot determine which class method to be called and even on calling which
46
class method gets the priority.
57
68
Declare the interfaces
79
*/
8-
interface Walkable
9-
{
10+
11+
interface Walkable {
1012
void walk();
1113
}
1214

13-
interface Swimmable
14-
{
15+
interface Swimmable {
1516
void swim();
1617
}
1718

1819
// Implement the interfaces in a class
19-
class Duck implements Walkable, Swimmable
20-
{
21-
public void walk()
22-
{
20+
class Duck implements Walkable, Swimmable {
21+
public void walk() {
2322
System.out.println("Duck is walking.");
2423
}
25-
public void swim()
26-
{
24+
25+
public void swim() {
2726
System.out.println("Duck is swimming.");
2827
}
2928
}
3029

3130
// Use the class to call the methods from the interfaces
32-
class MultipleInheritanceUsingInterfaces
33-
{
34-
public static void main(String[] args)
35-
{
31+
class MultipleInheritanceUsingInterfaces {
32+
public static void main(String[] args) {
3633
Duck duck = new Duck();
3734
duck.walk();
3835
duck.swim();
3936
}
40-
}
37+
}

0 commit comments

Comments
 (0)