Java Interface एक special type की class होती है जो केवल abstract methods (method signatures) को declare करती है। इसका इस्तेमाल हम एक contract की तरह करते हैं, जिसे कोई class implement करती है। यह Java की Object-Oriented Programming (OOP) का एक महत्वपूर्ण हिस्सा है।
Interface का मुख्य उद्देश्य है multiple inheritance को support करना और loosely coupled design बनाना। Java में class केवल एक ही class को extend कर सकती है, लेकिन multiple interfaces को implement कर सकती है। इससे flexibility और modularity बढ़ती है।
interface Vehicle {
void start();
void stop();
}
class Car implements Vehicle {
public void start() {
System.out.println("Car started");
}
public void stop() {
System.out.println("Car stopped");
}
}
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Barks");
}
}
class Cat implements Animal {
public void sound() {
System.out.println("Meows");
}
}
public class Test {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound(); // Output: Barks
Animal cat = new Cat();
cat.sound(); // Output: Meows
}
}
Java Interface एक powerful feature है जो program को modular, flexible और maintainable बनाता है। Interface के ज़रिए हम code में loosely coupled design ला सकते हैं और multiple inheritance की समस्या को हल कर सकते हैं। Beginners के लिए Interface सीखना OOP समझने का एक महत्वपूर्ण कदम है।