Java Interfaces

Introduction to Java Interfaces

Java interfaces का concept एक उदाहरण से समझने का प्रयास करते है। मान लीजिये आपका एक दोस्त है। और आपके दोस्त की एक कंपनी है। आप भी एक कंपनी खोलना चाहते है। आप अपने मित्र से पूछते है की मुझे कंपनी खोलने के लिए क्या करना होगा। आपका दोस्त आपको बताता है की sales department, production department, accounts और management department आपको setup करना होगा। ध्यान दीजिये आपके दोस्त ने सिर्फ आपको बताया है की आपको क्या करना है। आप ये कैसे करेंगे ये आप पर depend करता है। इसी तरह interfaces classes को बताते है की उन्हें क्या करना है। Class वो काम कैसे करती है ये उस class पर depend करता है।

Java interface उसी तरह है जैसे की आप exam time में सिर्फ topics की headings को याद करते है और एग्जाम में उन headings की detail अपने मन से लिख कर आते है। Interface किसी boss की तरह होता है और class किसी employee की तरह interface क्लास को काम बताता है और class उस काम को करती है। Interface कोई भी काम खुद नहीं करता है। Interface में define किये गए methods को आपको किसी task की तरह देखना चाहिए जो interface क्लास से करवाना चाहता है।

Interfaces में define किये गए सभी methods by default abstract होते है। मतलब इनकी कोई body नहीं होती है। Interface को हमेशा public declare किया जाता है। जब कोई class interface को implement करती है तो उसे interface में declare किये गए सभी methods की definition provide करनी होती है। आप किसी method को छोड़ नहीं सकते है। एक interface को कोई class extend नहीं करती है। Interfaces हमेशा implement किये जाते है। इसके लिए आप implements keyword यूज़ करते है।

Java में multiple inheritance allow नहीं है। लेकिन java में interfaces के द्वारा multiple inheritance implement किया जा सकता है। Interfaces super class की तरह methods provide करते है, बस उनकी definitions हमे implement करने वाली class में देनी होती है। Interfaces classes की तरह ही होते है, लेकिन आप इनका object नहीं create कर सकते है। और किसी method की body भी आप नहीं declare कर सकते है।

Methods के साथ ही आप interfaces में variables भी declare कर सकते है। Interfaces में declare किये जाने वाले variables static और final होने चाहिए। एक interface एक से ज्यादा interfaces को extend कर सकता है। एक बार interface declare करने के बाद कितनी भी classes उस interface को implement कर सकती है। और एक class जितने चाहे उतने interface implement कर सकती है। इसके लिए आपको सभी interfaces के नामों को (,) से separate करना होगा।

Interfaces classes के साथ Is-a relationship establish करते है। उदाहरण के लिए आपके pass एक interface है fruits नाम से जिसमे color और taste आदि methods है और एक mango नाम की class है। अब यदि mango class fruit interface को implement करती है तो इनमे Is-a relation होगा। यानि आप कह सकते है की Mango is a fruit.

Working with Interfaces

Interfaces को declare करना बिलकुल आसान होता है। इसके लिए आप interface keyword यूज़ करते है। पहले तो आप public access modifier define करते है, इसके बाद आप interface keyword लिख कर interface का नाम लिखते है। इसके बाद curly brackets में आप उन सभी methods को declare करते है जो आप किसी class से implement करवाना चाहते है। वैसे तो interfaces के अंदर सभी methods by default public होते है। लेकिन आप चाहे तो public access modifier सभी methods के आगे add कर सकते है। इसका उदाहरण नीचे दिया जा रहा है।

Structure:

public interface interface_Name
{
   // methods without body;
}

Example:

public interface yrFriend
{
     public void sales();
     public void production();
     public void accounts();
     public void management();
}

जैसे की मैने आपको पहले बताया implements keyword को यूज़ करते हुए कोई भी class आसानी से interface को implement कर सकती है। Interfaces का complete उदाहरण निचे दिया जा रहा है।

Example:

public interface yourFriend
{

     public void sales();
     public void production();
     public void accounts();
     public void management();
}


public class You implements yourFriend
{

     public void sales()
     {
         // implement sales department
     }

     public void production()
    {
        // implement production department
    }

    public void accounts()
   {
        // implement production department
   }

   public void management()
  {
      // implement production department
   }

}