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.java | Sample.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 :Output ://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(); } }
interface A
Reference Variable for interface
interface का reference variable create किया जा सकता है , लेकिन object; create नहीं किया जा सकता |
Source Code :Output ://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(); } }
interface A
Relation between Class and Interface
interface और interface में extends keyword का इस्तेमाल किया जाता है |
interface और class में implements keyword का इस्तेमाल किया जाता है |


Source Code :
Output ://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(); } }
interface A interface B
Multiple Inheritance using interface and class
यहाँ पर interface और class का multiple inheritance लिया है |


Source Code :
Output ://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(); } }
interface A interface B
Multiple Inheritance using Interface
यहाँ पर interface और interface का multiple inheritance लिया है |


Source Code :
Output ://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(); } }
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 होते है |