Lakhyadeep Sen’s Post

Interfaces also allow a class to follow multiple contracts at the same time. Unlike classes, where a class can extend only one parent class, a class in Java can implement multiple interfaces. Things that became clear : • a class can implement more than one interface • each interface can define a different set of behaviours • the implementing class must provide implementations for all the methods • this approach allows combining different capabilities in a single class • it helps achieve flexibility while avoiding multiple inheritance of classes A simple example shows how multiple interfaces can be implemented : interface ICalculator { void add(int a, int b); void sub(int a, int b); } interface IAdvancedCalculator { void mul(int a, int b); void div(int a, int b); } class CalculatorImpl implements ICalculator, IAdvancedCalculator { public void add(int a, int b) { System.out.println(a + b); } public void sub(int a, int b) { System.out.println(a - b); } public void mul(int a, int b) { System.out.println(a * b); } public void div(int a, int b) { System.out.println(a / b); } } This structure allows the class to support operations defined by multiple interfaces while keeping responsibilities organized. #java #oop #programming #learning #dsajourney

To view or add a comment, sign in

Explore content categories