extending Interface - multiple inheritance

 In Java, interfaces can be extended using the extends keyword. This allows you to build a more complex interface by inheriting from one or more existing interfaces.


✅ Syntax for Extending Interfaces


interface A { void methodA(); } interface B extends A { void methodB(); }

Here, interface B extends interface A. So any class that implements B must implement both methodA() and methodB().


✅ Example


interface A { void methodA(); } interface B extends A { void methodB(); } class MyClass implements B { public void methodA() { System.out.println("methodA from interface A"); } public void methodB() { System.out.println("methodB from interface B"); } } public class Main { public static void main(String[] args) { MyClass obj = new MyClass(); obj.methodA(); obj.methodB(); } }

✅ Extending Multiple Interfaces

Java supports multiple interface inheritance, so one interface can extend multiple interfaces.


interface X { void methodX(); } interface Y { void methodY(); } interface Z extends X, Y { void methodZ(); }

Any class that implements Z must provide implementations for methodX(), methodY(), and methodZ().


🚫 Note

  • You can only extend interfaces from other interfaces.

  • A class cannot extend an interface, it must implement it.

Comments

Popular posts from this blog

Object Oriented Programming PBCST304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Introduction to Java Programming

Inheritance and Polymorphism