Interfaces in Java जावा में इंटरफेस
An interface in Java is a kind of blueprint or structure for a class. Just like a class, it can have methods and variables, but the methods declared in an interface are abstract by default. In other words, an interface defines a contract that other classes must follow. Interfaces are used to achieve abstraction and multiple inheritance.
इंटरफेस, जावा में एक तरह की क्लास का ढांचा या ब्लूप्रिंट होता है। इसमें क्लास की तरह ही मेथड और वेरिएबल होते हैं, लेकिन इनमें घोषित मेथड डिफ़ॉल्ट रूप से एब्सट्रेक्ट होती हैं। इंटरफेस का उपयोग एब्सट्रेक्शन और मल्टीपल इनहेरिटेंस प्राप्त करने के लिए किया जाता है।
📌 Interface Features (इंटरफेस की विशेषताएं)
In newer versions of Java, interfaces can also contain private
, default
, and static
methods. Interfaces cannot be instantiated directly, but a class that implements an interface must provide implementations for all its abstract methods.
जावा के नए संस्करणों में, इंटरफेस में private
, default
, और static
मेथड्स भी हो सकती हैं। इंटरफेस का इंस्टेंस नहीं बनाया जा सकता, लेकिन जो क्लास इसे इम्प्लीमेंट करती है उसे इसकी सभी एब्सट्रेक्ट मेथड्स को डिफाइन करना जरूरी होता है।
🔧 Implementing Interfaces (इंटरफेस को इम्प्लीमेंट करना)
To implement an interface in Java, the implements
keyword is used.
interface Animal {
public void test();
}
class Cat implements Animal {
public void test() {
System.out.println("Test method of Animal interface is Implemented");
}
public static void main(String args[]) {
Animal a = new Cat();
a.test();
}
}
आउटपुट:
Test method of Animal interface is Implemented
इंटरफेस को इम्प्लीमेंट करने के लिए implements
कीवर्ड का उपयोग किया जाता है। ऊपर दिए गए उदाहरण में Cat
क्लास ने Animal
इंटरफेस की test()
मेथड को इम्प्लीमेंट किया है।
♻️ Multiple Inheritance Using Interfaces (इंटरफेस से मल्टीपल इनहेरिटेंस)
Java does not support multiple inheritance with classes, but we can achieve it using interfaces.
interface Eatable {
void eat();
}
interface Drinkable {
void drink();
}
class Food implements Eatable, Drinkable {
public void eat() {
System.out.println("this is eatable");
}
public void drink() {
System.out.println("this is drinkable");
}
public static void main(String args[]) {
Food obj = new Food();
obj.eat();
obj.drink();
}
}
आउटपुट:
this is eatable
this is drinkable
जावा में एक क्लास एक से अधिक इंटरफेस को इम्प्लीमेंट करके मल्टीपल इनहेरिटेंस को प्राप्त कर सकती है।
🔗 Extending Interfaces (इंटरफेस को एक्सटेंड करना)
An interface can extend another interface using the extends
keyword.
interface NewsPaper {
void news();
}
interface Magazine extends NewsPaper {
void colorful();
}
एक इंटरफेस दूसरे इंटरफेस को extends
कीवर्ड से एक्सटेंड कर सकता है, जैसा कि ऊपर उदाहरण में दर्शाया गया है।
✅ Benefits of Interface (इंटरफेस के लाभ)
-
Achieves full abstraction. सम्पूर्ण एब्सट्रेक्शन प्राप्त करने के लिए।
-
Supports multiple inheritance. मल्टीपल इनहेरिटेंस के लिए।
-
Helps in loose coupling between components. लूज़ कपलिंग के लिए सहायक।
⚔️ Interface vs Abstract Class (इंटरफेस बनाम एब्सट्रेक्ट क्लास)
Feature | Interface | Abstract Class |
---|---|---|
Methods | Only abstract (Java 8+ allows default/static) | Abstract + non-abstract |
Keyword | interface |
abstract |
Inheritance | Can extend only interfaces | Can extend class and implement interfaces |
Multiple Inheritance | Supported | Not supported |
Variables | Public, static, final only | Can be static, final, or instance |
Implementation | Cannot provide implementation | Can provide implementation |
इंटरफेस और एब्सट्रेक्ट क्लास में यह प्रमुख अंतर होते हैं, जो उनके उपयोग को परिभाषित करते हैं।
🧪 Program Example (प्रोग्राम उदाहरण)
class Example {
void view() {
System.out.println("Example class extends in Using class");
}
}
interface ex {
final double max = 100;
}
interface ex1 extends ex {
final double pi = 3.14;
void show();
}
interface ex2 {
final double PI = (22 / 7);
void display();
}
class Using extends Example implements ex1, ex2 {
double exa = max;
double ex1a = pi;
double ex2a = PI;
public void show() {
System.out.println("max=" + exa);
System.out.println("pi=" + ex1a);
}
public void display() {
System.out.println("PI=" + ex2a);
}
}
public class TestInterface {
public static void main(String args[]) {
Using u = new Using();
ex1 e1 = u;
e1.show();
ex2 e2 = u;
e2.display();
u.view();
}
}
इस प्रोग्राम में एक क्लास, एक क्लास को एक्सटेंड कर रही है और दो इंटरफेस को इम्प्लीमेंट कर रही है, जिससे इंटरफेस की शक्ति का प्रदर्शन होता है।
Comments
Post a Comment