आपकी ऑफलाइन सहायता

BACK
49

सी प्रोग्रामिंग

149

पाइथन प्रोग्रामिंग

49

सी प्लस प्लस

99

जावा प्रोग्रामिंग

149

जावास्क्रिप्ट

49

एंगुलर जे.एस.

69

पी.एच.पी.
माय एस.क्यू.एल.

99

एस.क्यू.एल.

Free

एच.टी.एम.एल.

99

सी.एस.एस.

149

आर प्रोग्रामिंग

39

जे.एस.पी.





डाउनलोड पी.डी.एफ. ई-बुक्स
Java - Interfaces

interface ये class के जैसे होते है | interface जो methods होते है, वो abstract होते है और उनका scope public होता है | interface का कोई object create नहीं किया जा सकता, लेकिन reference variable create किया जा सकता है | interface में जो methods होते है, उनका सिर्फ declaration किया जाता है |

interface में जब instance variable को intialized किया जाता है, तब उसके साथ bydefault compiler द्वारा public static final ये keywords आ जाते है |

interface को implement करने के लिए class के जरिये implements keyword का इस्तेमाल किया जाता है |

interface में constructor नहीं होते |

Syntax for interface

interface interface_name{
	//some code;
}

Example for interface

interface Sample{
	int a = 5;
	void disp();
}

Sample.javaSample.class
interface Sample{

int a = 5;
void disp();

}
Compiler interface Sample{

public static final int a = 5;
public abstract void disp();

}

Full Example for interface

जब program में interface का इस्तेमाल किया जाता है, तब abstract method का नियम लागू होता है |

For eg.

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

Source Code :
//B.java
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();  
	}
}  
Output :
interface A

Reference Variable for interface

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

Source Code :
//B.java
interface A
{
    void disp();
}

class B implements A
{
    public void disp() 
    {
     System.out.println("interface A");
    }
	
    public static void main(String args[]) 
    {
     A obj = new B();  //obj is reference variable of interface A
     obj.disp();
    }
}
Output :
interface A

Relation between Class and Interface

interface और interface में extends keyword का इस्तेमाल किया जाता है |

interface और class में implements keyword का इस्तेमाल किया जाता है |



Source Code :
//C.java
interface A
{
    void disp1();
}

interface B extends A{

    void disp2(); 
}
class C implements B{
	
	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();
    }
}
Output :
interface A
interface B

Multiple Inheritance using interface and class

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


Source Code :
//C.java
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();
    }
}
Output :
interface A
interface B

Multiple Inheritance using Interface

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


Source Code :
//D.java
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();
    }
}
Output :
interface A
interface B
interface C

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 होते है |