Wednesday, 17 October 2018

Java Interface का परिचय (Introduction of Java Interface)


Java Interface का परिचय
(Introduction of Java Interface)

Interface को एक उदाहरण के द्वारा समझते हैं मान लीजिये आप अपने एग्जाम टाइम में किसी भी subject के topic कि हैडिंग को याद करके जाते है और एग्जाम में उस हैडिंग आप खुद अपने मन से उत्तर बना कर लिख आते हैं | ऐसा ही कुछ जावा interface में होता है जावा interface, जावा class को ये बताता है कि क्या काम करता है class को ये नहीं बताता उसे ये काम कैसे करना हैं class खुद अपने तरीके से उस काम को करती है |

अगर सीधे शव्दों में कहें तो interface कोई भी काम खुद नहीं करता है interface अपना काम class से करवाता है |

 interface class को ये बताता है कि class को क्या काम करना है पर ये नहीं बताता class को वो काम कैसे करना है class खुद उस काम को अपने तरीके से करती है | Interface में define किये गए methods में एक task / operation होता है जो interface, class से करवाना चाहता है |

Interface में define किये गए सभी methods by default abstract होते हैं जिसका मतलब ये कि उन methods कि कोई body नहीं होती है |

interface हमेशा public declare किये जाते हैं जब कोई class किसी interface को implement करती है तो उसे interface के सभी methods की जानकारी class को देनी पड़ती है | आप interface के किसी भी एक method छोड़ नहीं सकते हैं सभी methods कि जानकारी provide करनी होती है |

Interface में आप method के साथ – साथ variables भी declare कर सकते हैं ये variables सिर्फ static और final ही declare किये जा सकते हैं | Interface को किसी भी class के द्वारा inherit नहीं किया जा सकता है उसे सिर्फ implement keyword के द्वारा class में implement किया जा सकता है |

जावा multiple inheritance को support नहीं करता है लेकिन interfaces के द्वारा multiple inheritance को implement किया जा सकता है |

एक interface एक से ज्यादा interfaces को extends keyword के द्वारा inherit कर सकता है | एक बार interface declare हो जाने के बाद एक से जयादा classes उस interface को implement कर सकती है इसके लिए आपको interfaces के नामों के बीच में comma (,) लगा कर separate करना होगा |

इंटरफ़ेस का इस्तेमाल क्यों किया जाता हैं? (Use of interface in java in Hindi)
  • जावा प्रोग्रामिंग, मल्टीपल इनहेरिटेंस को सपोर्ट नहीं करती, लेकिन इंटरफ़ेस का इस्तेमाल करते हुए मल्टीपल इनहेरिटेंस को हासिल किया जा सकता हैं।
  • इंटरफ़ेस का इस्तेमाल लूज लूपिंग को प्राप्त करने हेतु भी किया जाता हैं।
  • इंटरफ़ेस का इस्तेमाल abstraction  को हासिल करने के लिए किया जाता हैं।

Interface को implement कैसे करते हैं?

(Implementation of Interface in Hindi)

इंटरफ़ेस के implement को आसानी से समझने के लिए, एक उदाहरण देखते हैं। मान लीजिए दो क्लास आपस में कॉन्ट्रैक्ट साइन कर रहे हैं। इस कॉन्ट्रैक्ट द्वारा वे आपस में सहमत हैं कि interface में दीए गए सभी फंक्शन्स परफॉर्म किए जाएँगे। लेकिन अगर फंक्शन्स परफॉर्म नहीं किए जाते, तब क्लास को खुदको abstract घोषित करना होगा।
interface को क्लास में इस्तेमाल करने के लिए implements इस कीवर्ड का इस्तेमाल किया जाता हैं। इस keyword का इस्तेमाल क्लास declare करते समय किया जाना जरुरी हैं।

interface Animal
{
public void eat();
public void travel();
}
public class Mammal implements Animal
{
public void eat()
{
System.out.println(“Mammal eats”);
}
public void travel()
{
System.out.println(“Mammal travels”);
}
public static void mian(String args[])
{
Mammal m=new Mammal();
m.eat();
m.travel();
}
}
 
जैसा की आप देख सकते हैं, ऊपर दिए गए उदाहरण में हमने एक interface declare किया हैं, उसका नाम Animal यह हैं। उसमें हमने दो फंक्शन भी declare किए हैं, लेकिन इंटरफ़ेस में हमने उन फंक्शन्स को डिफाइन नहीं किया हैं।

आग बढ़ते हुए हमने, एक क्लास भी declare किया है और उस क्लास में इंटरफ़ेस Animal को implements keyword की मदद  से इस्तेमाल किया हैं। इस क्लास में हमने इंटरफ़ेस Animal में डिक्लेअर 

सभी फंक्शन को डिफाइन कर, उनका इस्तेमाल किया हैं। इसी प्रकार आप, किसी भी इंटरफ़ेस को किसी भी क्लास में इस्तेमाल कर सकते हैं, लेकिन ध्यान रहे की implements keyword  का इस्तेमाल, क्लास डिफाइन करते समय करना न भूलें।

जावा interface के गुणधर्म
(Property of Java Interface)

1.       Interface by default बस्त्रक्ट होता है इसमें आपको abstract keyword का इस्तेमाल करने कि जरुरत नहीं होती है |
2.       interface में declare किये गए सभी function, methods और variables भी by default abstract होते हैं |
3.       interface और और उसके सभी methods और variables by default abstract होते हैं फिर भी interface के सभी function public होते हैं |

Example For Interface

Program में देखे तो interface A में disp() नाम का method आया है | ये bydefault public और abstract keywords के साथ होता है | जब interface पर कोई method declare की जाती है, तब उसके sub-class पर उसकी body देना जरुरी होती है |

interface A{ 
                   
                    void disp();
}
class B implements A{ 

                    public void disp(){
                   
                    System.out.println("interface A");
} 
                    public static void main(String args[]){
                                        
                    B b = new B(); 
                    b.disp(); 
                    }
} 



Reference Variable for Interface

Interface का reference variable create किया जा सकता है , लेकिन object; create नहीं किया जा सकता |

interface Animal
{
public void eat();
public void travel();
}
public class Mammal implements Animal
{
public void eat()
{
System.out.println(“Mammal eats”);
}
public void travel()
{
System.out.println(“Mammal travels”);
}
public static void mian(String args[])
{
Mammal m=new Mammal();
m.eat();
m.travel();
}
}

Multiple Inheritance using interface and class

यहाँ पर interface और class का multiple inheritance लिया है |


interface A{
                    
    void disp1();
}
interface B{
 
    void disp2(); 
}
class C implements B, A{
                    
                    public void disp1(){
                                         System.out.println("interface A");
                    }
                    public void disp2(){
                                         System.out.println("interface B");
                    }
 
    public static void main(String args[]) 
    {
     C obj = new C();
     obj.disp1();
                     obj.disp2();
    }
}


Multiple Inheritance using Interface

यहाँ पर interface और interface का multiple inheritance लिया है |


interface A{
                    
    void disp1();
}
 
interface B{
 
    void disp2(); 
}
interface C extends B, A{
                    
                    void disp3();
}
                    
class D{
                    
                    public void disp1(){
                                         System.out.println("interface A");
                    }
                    public void disp2(){
                                         System.out.println("interface B");
                    }
                    public void disp3(){
                                         System.out.println("interface C");
                    }
 
    public static void main(String args[]) 
    {
     D obj = new D();
     obj.disp1();
                     obj.disp2();
                     obj.disp3();
    }
}


Interface के कुछ नियम
  • Interface का object create नहीं किया जा सकता, लेकिन reference variable create किया जा सकता है |
  • Interface का constructor नहीं होता |
  • Interface और Interface को extend किया जाता है |
  • Interface और class को implement किया जाता है |
  • Interface का हर reference variable public, static और final keyword के साथ होता है |
  • Interface में हर instance variable; final keyword के साथ होने के कारण constant होता है |
  • Interface के methods public और abstract होते है |



धन्यवाद अगर आपको हमारी पोस्ट अच्छी लगी हो तो प्लीज कोम्म्मेंट बॉक्स में जरुर बताएं और अपने दोस्तों के साथ पोस्ट जरुर शेयर करें |

2 comments: