← Back to Core Java Course | Chapter 9: OOP Advanced | Lesson 5 of 8

Java के कई Interfaces

एक class एक साथ कई interfaces के लिए sign up कर सकती है, एक student और एक swimmer दोनों वाले एक person जैसा। इसे फिर हर एक की मांग की हर चीज़ करनी चाहिए।
Syntax
java
class ClassName implements InterfaceA, InterfaceB {
    // implement all methods of both interfaces
}

Implementing Multiple Interfaces

जबकि एक Java class सिर्फ एक parent class extend कर सकती है, यह एक साथ ज़रूरत के अनुसार कितने भी interfaces implement कर सकती है, implements के बाद हर interface नाम को एक comma से अलग करते हुए — यह multiple class inheritance बनाने वाली ambiguity problems से बचता है।

उदाहरण: Implementing Multiple Interfaces

java
// Define the interface Flyable
interface Flyable {
	void fly();
}
// Define the interface Swimmable
interface Swimmable {
	void swim();
}
// Define the class Duck
class Duck implements Flyable, Swimmable {
	public void fly() { System.out.println("Flying"); }
	public void swim() { System.out.println("Swimming"); }
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Duck object and store it in duck
		Duck duck = new Duck();
		duck.fly();
		duck.swim();
	}
}

Interface Inheritance

Interfaces खुद extends उपयोग करके दूसरे interfaces extend कर सकते हैं, आपको layered contracts बनाने देते हुए जहां एक ज़्यादा specific interface एक ज़्यादा general एक की requirements inherit और जोड़ता है।

उदाहरण: Interface Inheritance

java
// Define the interface Animal
interface Animal {
	void eat();
}
// Define the interface Pet
interface Pet extends Animal {
	void play();
}
// Define the class Dog
class Dog implements Pet {
	public void eat() { System.out.println("Eating"); }
	public void play() { System.out.println("Playing"); }
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		new Dog().eat();
	}
}

Multiple Interface Inheritance

एक interface एक साथ कई parent interfaces भी extend कर सकता है (interfaces में classes जैसी single-inheritance restriction नहीं), commas से अलग हर parent interface नाम list करते हुए, और extending interface उनकी सभी abstract method requirements combined inherit करता है।

उदाहरण: Multiple Interface Inheritance

java
interface A {
	void methodA();
}
interface B {
	void methodB();
}
interface C extends A, B { // extends both A and B
}
class Impl implements C {
	public void methodA() { System.out.println("A"); }
	public void methodB() { System.out.println("B"); }
}
public class Main {
	public static void main(String[] args) {
		new Impl().methodA();
	}
}

Method Name Collisions

अगर एक class दो interfaces implement करती है जो संयोग से identical signature वाला एक method declare करते हैं, class को दोनों requirements एक साथ satisfy करने के लिए सिर्फ एक implementation लिखनी है — Java इसे एक single method मानता है जो दो contracts एक साथ fulfill करता है।

उदाहरण: Method Name Collisions

java
interface Flyable {
	void move();
}
interface Swimmable {
	void move();
}
class Duck implements Flyable, Swimmable {
	public void move() { // one implementation satisfies both
		System.out.println("Duck moves");
	}
}
public class Main {
	public static void main(String[] args) {
		new Duck().move();
	}
}

Solving Multiple Inheritance limits

Multiple interfaces classic "diamond problem" से बचते हैं जो multiple class inheritance का कारण बन सकती है, क्योंकि interfaces (default method bodies को छोड़कर) असली field state नहीं रखते — conflict करने के लिए कोई ambiguous shared data नहीं, सिर्फ satisfy करने के लिए method contracts।

उदाहरण: Solving Multiple Inheritance limits

java
interface Flyable {
	void fly();
}
interface Swimmable {
	void swim();
}
class Duck implements Flyable, Swimmable { // no diamond problem: no shared field state
	public void fly() { System.out.println("Flying"); }
	public void swim() { System.out.println("Swimming"); }
}
public class Main {
	public static void main(String[] args) {
		new Duck().fly();
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. उसी default method वाले दो interfaces, जो एक compile error है जब तक class इसे override न करे।
  2. Interfaces वाली class के लिए extends उपयोग करना, जब एक class implements उपयोग करती है।
  3. Interfaces में से किसी एक से एक method भूल जाना।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.