Java Interfaces In Hindi (Java Interfaces क्या है ?)

Java Interfaces In Hindi



Java Interfaces In Hindi :

Java में interface एक class का blueprint होता है. Class की तरह ही इंटरफ़ेस में methods और variables होते हैं परन्तु interface में declare किये गये methods, abstarct होती हैं.
दूसरे शब्दों में कहें तो, “ जावा में Interface एक reference type होता है और इसके पास abstract methods और static constants होते हैं. जावा में इसका प्रयोग abstraction और inheritance को प्राप्त करने के लिए किया जाता है.”

इंटरफ़ेस के पास केवल abstarct methods होते हैं परन्तु इसके पास method body नहीं होती है.

यह IS-A relationship को प्रस्तुत करता है.

इसे abstract class की तरह instantiate नहीं किया जा सकता. अर्थात हम interface के instance को create नहीं कर सकते.

java 8 से, interface के पास default और static methods भी हो सकते हैं.

java 9 से, इसके पास private methods भी हो सकते हैं.

Advantage of Interface – इंटरफ़ेस के फायदे

इसके लाभ निम्नलिखित हैं:-

  • इसका प्रयोग सम्पूर्ण abstraction को achieve (प्राप्त) करने के लिए किया जाता है.
  • इसके द्वारा हम multiple inheritance को प्राप्त कर सकते हैं.
  • इसका प्रयोग loose coupling को achieve करने के लिए भी किया जाता है.

Interface को कैसे declare किया जाता है?

एक इंटरफ़ेस को interface keyword का प्रयोग करके declare किया जाता है. यह सम्पूर्ण abstraction प्रदान करता है इसका मतलब यह है कि interface में सभी methods को empty body के साथ declare किया जाता है |

Syntax :

interface interface_name {  
// fields
// abstract/private/default methods
}

Interface के कुछ महत्वपूर्ण points –

नीचे आपको इसके कुछ important points दिए गये है जिन्हें हमेशा ध्यान में रखना चाहिए.

  1. हम interface के instance को create नहीं कर सकते परन्तु हम इसके reference को बना सकते है जो कि implement की गयी class के object को refer करता है.
  2. एक java class बहुत सारें interfaces को implement कर सकती है.
  3. एक इंटरफ़ेस एक से ज्यादा interfaces को extend कर सकता है.
  4. एक class जो interface को implement करती है उसे interface में declare की गयी सभी methods को implement करना आवश्यक होता है.
  5. इसमें सभी methods- abstract और public होती है और सभी fields- public, static और final होते हैं.
  6. एक इंटरफ़ेस दूसरे इंटरफ़ेस को implement नहीं कर सकता.
  7. वह interface जो दूसरे interface में declare होता है उसे nested interface कहते हैं.
  8. इसमें declaration के समय, variable को initialize करना आवश्यक होता है अन्यथा कम्पाइलर error throw करेगा.
  9. एक class उन दो interfaces को implement नहीं कर सकता जिनके methods का same name होता है परन्तु return type अलग-अलग होते हैं.

Interface implementation :

java में interface को implement करने के लिए implements keyword का प्रयोग किया जाता है | नीचे दिए गये उदाहरण में हमने Animal नाम के interface को create किया है और उसे Dog class के द्वारा implement किया है |

Example :

interface Animal{
  public void test();
}
class Dog implements Animal{
   public void test(){
     System.out.println("Interface Method Implemented");
  }
   public static void main(String args[]){
     Animal a = new Dog();
     a.test();
  }
}

Output :

Interface Method Implemented

Interface में multiple inheritance :

java में एक क्लास multiple inheritance को support नहीं करती है परन्तु एक class बहुत सारें interfaces को implement कर सकती है |

नीचे दिए example में एक class के द्वारा दो interfaces को implement किया गया है |

Example :

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();  
 }  
}  

Output :

this is eatable
this is drinkable.

Extending Interface :

जावा में, एक Interface दूसरे Interface को extend करता है. इसके लिए extends keyword का प्रयोग किया जाता है |

interface NewsPaper
{
 	news();
}

interface Magazine extends NewsPaper
{
 	colorful();
}

Difference between Abstract Class and Interface in Hindi :

Abstract ClassInterface
Abstract class के पास abstract और non-abstract दोनों प्रकार के methods हो सकते हैं.Interface के पास केवल abstract methods होती हैं. Java 8 से इसके पास default और static methods भी हो सकती हैं.
यह multiple inheritance को सपोर्ट नहीं करता.यह multiple inheritance को सपोर्ट करता है.
इसके पास final, non-final, static और non-static variables हो सकते हैं.इसके पास केवल static और final variables होते हैं.
यह इंटरफ़ेस का implementation प्रदान करती है.यह abstract class के implementation को provide नहीं करती है.
abstract कीवर्ड का प्रयोग abstract class को declare करने के लिए किया जाता है.interface कीवर्ड का प्रयोग इंटरफ़ेस को declare करने के लिए किया जाता है.
एक abstract class दूसरी java class को extend कर सकती है और बहुत सारें interfaces को implement कर सकता है.एक इंटरफ़ेस केवल दूसरे इंटरफ़ेस को extend कर सकता है.



अगर आपको यह पोस्ट 📑 पसंद आई हो तो अपने मित्रों के साथ जरूर शेयर करें। धन्यवाद !
Maxon

Hello there! I'm Maxon, a dedicated UI/UX designer on a mission to transform digital experiences into intuitive, user-centric journeys. With a keen eye for detail and a passion for crafting aesthetically pleasing interfaces, I strive to create designs that not only look stunning but also enhance usability and functionality.

Post a Comment

Previous Post Next Post