Java के कई Interfaces
In this page:
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
// 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();
}
}
Login to try C/C++/Java/PHP code in the editor
Interface Inheritance
Interfaces खुद extends उपयोग करके दूसरे interfaces extend कर सकते हैं, आपको layered contracts बनाने देते हुए जहां एक ज़्यादा specific interface एक ज़्यादा general एक की requirements inherit और जोड़ता है।
उदाहरण: Interface Inheritance
// 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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Method Name Collisions
अगर एक class दो interfaces implement करती है जो संयोग से identical signature वाला एक method declare करते हैं, class को दोनों requirements एक साथ satisfy करने के लिए सिर्फ एक implementation लिखनी है — Java इसे एक single method मानता है जो दो contracts एक साथ fulfill करता है।
उदाहरण: Method Name Collisions
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
- उसी default method वाले दो interfaces, जो एक compile error है जब तक class इसे override न करे।
- Interfaces वाली class के लिए
extendsउपयोग करना, जब एक classimplementsउपयोग करती है। - Interfaces में से किसी एक से एक method भूल जाना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: