Java Interface Example

Java Interface Example: सामान्‍यतया एक Abstract Class में कुछ Methods को Abstract Declare किया जाता है, जबकि कुछ Methods को सामान्‍य Method ही रखा जाता है। यदि हम कोई ऐसी Abstract Class Define करें, जिसमें सभी Methods Abstract हों, तो वास्तव में हम एक ऐसी Class Create कर रहे होते हैं, जिसे सम्भवतया Interface कहा जा सकता है। यानी Interface एक पूरी तरह से Abstract Class होती है, जिसमें किसी भी Method को Implement नहीं किया गया होता है।

हम किसी Interface को भी ठीक उसी तरह से Derive कर सकते हैं, जिस तरह से किसी अन्‍य सामान्‍य Class को Derive करते हैं। Interface भी एक सामान्‍य Class की तरह ही Class होती है। यानी इसमें भी Data Members व Methods होते हैं। लेकिन इसके Data Members हमेंशा static final यानी Constants होते हैं, जबकि इसके सभी Methods पूरी तरह से Unimplemented होते हैं।

इसके अलावा एक Interface की जो दूसरी सबसे बडी विशेषता होती है वह ये होती है कि जावा की कोई भी Class एक से ज्यादा यानी Multiple Interfaces को Implement कर सकता है। Interface की इस विशेषता के कारण जावा की Multiple Inheritance की कमी की भी पूर्ति हो जाती है।

यानी हालांकि जावा Directly Multiple Inheritance को Support नहीं करता है, लेकिन Interface को जावा में Multiple Inheritance के Alternate के रूप में Use किया जाता है, जिससे जावा में Multiple Inheritance की कमी नहीं लगती है।

चूंकि एक Class एक से ज्यादा Interfaces को Implement कर सकता है, इसलिए वह Class Interfaces में Declare किए गए सभी Classes के Methods को Implement करके Interface में Declared सभी Methods की Functionality Provide करने में सक्षम हो जाता है।

हालांकि ये तरीका पूरी तरह से Multiple Inheritance की सभी सुविधाएं प्रदान नहीं कर पाता है, फिर भी ये जावा का एक बहुत ही उपयोगी Feature है और ये Feature यही है जो जावा की Abstract Classes को Interfaces से Separate करता है।

जावा हमें ये सुविधा देता है कि हम विभिन्न Interfaces को यानी Public Methods को बिना Implement किए पूरी तरह से एक Abstract Form में Specify कर सकते हैं। फिर Interface का प्रयोग करके हम इन बिना Implemented Interfaces के समूह को किसी भी Class के लिए Implement कर सकते हैं। वास्तव में Interfaces Abstract Classes के समान ही होते हैं, लेकिन इनमें एक Extra Ability ये होती है कि कोई भी Class एक से ज्यादा Interfaces को Implement कर सकता है। जबकि Inheritance में एक Abstract Class से ही किसी एक Class को Inherit किया जा सकता है।

interface Keyword का प्रयोग करके हम Interface को उसके Implementation से पूरी तरह से Abstract कर सकते हैं। एक Interface में हम ये Specify करते हैं कि उसे Use करने वाली Class क्या-क्या करने में सक्षम हो सकेगा। Interface में हम ये Specify नहीं करते हैं, कि Interface उन कामों को कैसे Perform करेगा, जिन्हें Interface से प्राप्त करेगा।

Interface व Class को Create करने के Syntax में कोई विशेष अन्तर नहीं होता है। लेकिन Interface में केवल Constant Variables ही Declare किए जाते हैं और Interface में Declare किए गए Methods की Body नहीं होती है।

इसका फायदा ये होता है कि हम जिस Class में किसी Interface को प्राप्त करना चाहते हैं, उसी Class में उस Interface के सभी Methods को Implement कर लेते हैं। एक Interface को एक बार Define करने के बाद उस Interface को हम जितनी Classes में चाहें उतनी Classes में Implement कर सकते हैं। साथ ही हम एक ही Class में एक से ज्यादा Interfaces को Implement कर सकते हैं।

एक Interface को Implement करने के लिए हमें Interface में Declare किए गए सभी Abstract Methods को Class में पूरी तरह से Define करना होता है और हर Class किसी एक Interface को अपनी आवश्‍यकतानुसार Define करने के लिए पूरी तरह से स्वतंत्र होती है।

interface Keyword प्रदान करके जावा हमें ये सुविधा देता है कि हम “One Interface, Multiple Methods” प्रकार के Polymorphism को पूरी तरह से Utilize कर सकें। क्योंकि Interfaces को इस तरह से Design किया गया है, जिससे हमें जावा में Run Time में Dynamic Method Resolution यानी Program के Run Time में ये तय करने की सुविधा मिलती है कि किस स्थिति में कौनसा Method Execute होना चाहिए।

सामान्‍यतया एक Class से दूसरी Class में Methods के Call होने के लिए दोनों Classes का जावा Compiler पर Compile Time में उपलब्ध होना जरूरी होता है, ताकि जावा Compiler दोनों Classes के Method Signatures को Compatibility के लिए Check कर सके। जावा के Classing करने के इस तरीके में हम एक Limit में Bound हो जाते हैं, क्योंकि किसी भी Sub Class की Compiling के लिए उसकी सभी Super Classes की जरूरत होती है। Interfaces को इस कमी को पूरा करने के लिए Develop किया गया है।

Interfaces Method या Method के समूह के Implementations को Inheritance Hierarchy से Disconnect कर देता है। क्योंकि Interfaces Classes की तुलना में एक अलग Hierarchy में Represent होते हैं, इसलिए Interfaces Hierarchy से Unrelated विभिन्न प्रकार की Classes Hierarchy समान प्रकार के Interfaces को Implement कर सकने में सक्षम हो जाती हैं। इसीलिए हम एक ही Interface को अलग-अलग Classes के लिए Implement कर सकते हैं।

Java Interface Example : Declaration

जिस प्रकार से हम कोई Class Define करते हैं, उसी तरह से हमें Interface भी Define करना होता है। एक Interface को हम निम्न Syntax के अनुसार Create कर सकते हैं:

	access interface InterfaceName
	{
		Return_Type Method_Name(Parameter_List); 	//Un-Implemented Methods

		static final Type constantName =  Value;	//Constant Data Members
	}

इस Syntax में access Keyword या तो public होता है या फिर इसे Specify नहीं किया जाता है। जब इसे Specify नहीं किया जाता है, तब यहां Default Access Specifier Friend होता है और हम इस Interface को केवल समान Package की Classes में ही Use कर सकते हैं।

जबकि public Access Specifier वाले Interface को हम किसी भी Package की Class के साथ Use कर सकते हैं। interface एक Keyword है, जो Interface Create करने का काम करता है। InterfaceName हमारे Interface का वह नाम होता है, जिससे हम इसे Access करते हैं। ये नाम कोई भी Valid Identifier Name हो सकता है।

चूंकि एक Interface में सभी Methods Abstract होते हैं, इसलिए Methods के Declaration में abstract Keyword को Use करने की कोई जरूरत नहीं रहती है। विभिन्न Abstract Methods के Parenthesis के बाद Semicolon का प्रयोग करके उन्हें Terminate कर दिया जाता है। इन्हें Implement नहीं किया जाता है, इसलिए इनकी Body नहीं होती है। हम किसी Interface में किसी भी Method को Implement नहीं कर सकते हैं।

यदि हम किसी Interface में किसी Method को Implement या Define करते हैं, तो Compiler Error Generate करता है, क्योंकि एक Interface के सभी Methods Abstract होते हैं, जिन्हें उसी Class में Implement किया जाता है, जिसमें Interface को Use करना होता है।

साथ ही जिस किसी भी Class में हमें जिस किसी भी Interface को Use करना होता है, उस Class में हमें Interface के सभी Abstract Methods को Implement करना जरूरी होता है। हम Interface के किसी भी Method को बिना Implement किए नहीं छोड सकते हैं। यदि हम ऐसा करते हैं, तो Compiler Error Generate करता है।

Interfaces में हम Variables भी Declare कर सकते हैं, लेकिन किसी Interface में हम जो भी Variable Declare करते हैं, वे Variables हमेंशा के लिए Public, StaticFinal यानी Constants होते हैं। इसलिए इन्हें Create करते ही हमें इन्हें Initialize भी करना पडता है और चूंकि किसी Interface के सभी Variables Constant होते हैं, इसलिए Interface को किसी Class में Implement करते समय भी हम इन Variables के मान को Change नहीं कर सकते हैं।

यदि किसी Interface को Public Declare किया जाता है, तो उसके सभी Data MembersMethods By Default Public हो जाते हैं। Method का Public होना इसलिए भी जरूरी होता है, क्योंकि जिस Class में भी हम Interfaces को Use करते हैं, हमें उस Class में Interface के सभी Members को Implement करने के लिए Access करना ही पडता है।

इसलिए यदि इन्हें Public ना रखा जाए, तो हम किसी Interface के किसी Method को किसी Class में Implement नहीं कर सकेंगे, जिससे Interface का Meaning ही खत्म हो जाएगा। किसी Interface को हम निम्नानुसार Define कर सकते हैं:

	public interface CalculateArea
	{
		void area();
	}

Java Interface Example : Extending

Classes की तरह ही हम किसी Interface को भी Extend करके नया Sub Interface Create कर सकते हैं। हम जब भी किसी Interface को Extend करते हैं तो Sub Interface में Super Interface के सभी Abstract Methods व Data Members Inherit हो जाते है। Interfaces को Extend करने के लिए भी हमें वही Syntax Use करना पडता है, जिस Syntax का प्रयोग हम Class को Extend करने के लिए करते हैं।

	interface SubInterface extends SuperInterface
	{
		// Constant Data Members
		// Abstract Method
	}

उदाहरण के लिए यदि हम किसी Interface में निम्नानुसार केवल Constants को ही Define करें:

interface ItemInformation{
	public static int code = 1000;
	public static String name = “Freeze”;
}

तो इस Interface को हम निम्नानुसार Extend कर सकते हैं:

	interface ItemInformation
	{
		int code = 1000;
		String name = “Freeze”;
	
		// Abstract Methods
	}
	
	interface Item extends ItemInformation
	{
		// Constant Data Members
	
		void display();
	}

जब हम इस प्रकार से किसी Interface को Inherit करते हैं, तब Sub Interface Item में Super Interface ItemInformation के सभी Data Members व Methods Inherit हो जाते हैं। हम देख सकते हैं कि हमने ItemInformation Interface में दोनों Data Members को सामान्‍य Variables की तरह Declare किया है। हम ऐसा कर सकते हैं, लेकिन हम चाहे public static Keywords का प्रयोग करें चाहे ना करें, Interface के सभी Data Members By Default Constant ही होते हैं।

हालांकि हम कई Classes से एक Sub Class को एक बार में Extend नहीं कर सकते क्योंकि जावा Multiple Inheritance को Support नहीं करता है। लेकिन हम कई Interfaces से एक नए Sub Interfaces को एक बार में Extend कर सकते हैं, इस प्रक्रिया को जावा Support करता है। यानी हम चाहें तो Interfaces को निम्नानुसार Extend कर सकते हैं:

	interface DataMemberInfo
	{
		int code = 1000;
		String name = “Freeze”;
	
		// Abstract Methods
	}
	
	interface MemberMethodInfo
	{
		// Constant Data Members
	
		void display();
	}
	
	interface MultipleInterface extends DataMemberInfo, MemberMethodInfo
	{
		int code = 1000;
		String name = “Freeze”;
	
		// Abstract Methods
	}

हालांकि हम एक Sub Interface में एक से ज्यादा Interfaces को Inherit कर सकते हैं, लेकिन फिर भी हम Sub Interface में किसी भी Super Interface के किसी भी Method को Define नहीं कर सकते हैं। सभी Sub Interfaces अभी भी Interface की तरह ही Behave करते हैं, Class की तरह नहीं।

साथ ही जब हम किसी Interface को Implement करते हैं, तो हमें उस Interface के सभी Abstract Methods को Class में Implement करना पडता है। लेकिन जब हम किसी Sub Interface को Class में Implement करते हैं, तब हमें उस Sub Interface के तो सभी Methods को Class में Implement करना ही होता है, साथ ही उस Sub Interface की सभी Super Interfaces के Abstract methods को भी हमें Class में Implement करना जरूरी होता है। जब हमें किसी Sub Interface में एक से ज्यादा Interfaces को Extend करना होता है, तब सभी Super Interfaces को Comma Operator द्वारा Separate रखा जाता है।

एक बात हमेंशा ध्‍यान रखें कि हम कभी भी किसी Interface में किसी Class को Inherit नहीं कर सकते हैं। क्योंकि Class को Inherit करने पर Class के Implemented Methods Interface में Inherit हो जाएंगे, जो कि Interface के नियम के खिलाफ है।

Java Interface Example : Implementing

क्योंकि Interface किसी भी Class के लिए केवल एक Declaration, Prototype या Template होता है, इसलिए हमें इस Prototype को उसी Class में Implement करना होता है, जिस Class में हमें उसे Use करना होता है। किसी Interface को Implement करना उस Class को Derive करने के समान ही होता है। लेकिन किसी Class को Derive करना या नहीं करना हमारी इच्छा पर निर्भर करता है जबकि किसी Interface को Use करने के लिए हमें उसके सभी Methods को अपनी उस Class में Implement करना ही होता है, जिसमें हम Interface को Use करना चाहते हैं।

किसी Interface को किसी Class में Implement करने के लिए हमें implements Keyword का प्रयोग करना होता है। किसी Interface के Method को किसी Class में Implement करने का Syntax निम्नानुसार होता है:

	class ClassName [extends SuperClass] 
		implements InterfaceName1 [,InterfaceName2, ..., InterfaceNameN]
	{
		// Class Members
		// Methods
	}

यहां ClassName Create होने वाली नई Class है। यदि Class किसी दूसरी Super Class से Inherit हो रहा हो, तो हमें Class को Extend करने वाले Syntax के बाद Implement Syntax को लिखना होता है। InterfaceName वह Interface है, जिसे ClassName नाम की Class में Implement किया जा रहा है।

हम एक बार में केवल एक ही Super Class को Inherit कर सकते हैं, लेकिन हम एक बार में एक से ज्यादा Interfaces को Extend कर सकते हैं। जावा के Interfaces की इसी सुविधा के कारण हम जावा में Multiple Inheritance Concept के Alternate के रूप में Interfaces को Use कर सकते हैं।

अगले Program में हमने सबसे पहले Area नाम का एक Interface Create किया है। इस Interface को किसी भी Shape का Area Calculate करने के लिए Implement किया जा सकता है। इसी Program में आगे हमने RectangleCircle नाम की दो Classes Create की हैं और इन Classes के Objects का Area Calculate करने के लिए Interface को इन दोनों Classes में Implement किया गया है। इस Program में हमने Rectangle व Circle दोनों Classes के new Operator का प्रयोग करके Object Create किए हैं, जबकि इसी Program में हमने Interface Area Type का एक Reference Create किया है।

हम Interface Type का Object Create कर सकते हैं, क्योंकि ये भी एक तरह की Class ही है, लेकिन Pure Abstract Class है। फिर इस Interface Area Type के Reference में हमने Rectangle Class के Object का Reference Area Type के Reference में Assign करके computArea() नाम के Method को Call किया है। ये Method Interface Class में Declare किया गया Abstract Method है, इसलिए इसे Rectangle Class में Implement किया गया है।

जब Interface Area Class के Object में Rectangle Class का Reference होता है, तब जावा Rectangle Class के computArea() Method को Execute करके Rectangle Object का Area Calculate कर देता है।

इसी तरह से Interface के computArea() Method को Circle Class में भी Implement किया गया है, इसलिए जब हम Circle Class के Object का Reference Area Interface Class के Object को देते हैं और उसके Reference में computArea() Method को Call करते हैं, तो क्योंकि Interface Area के Object में Circle Class का Reference होता है, इसलिए जावा Circle Class के computArea() Method को Call करके Area Class के Object का Area Calculate कर लेता है।

// Program
	// File Name: InterfaceImplementation.java
	interface Area {
		final static float PI = 3.14f;
	
		float computeArea(float x, float y);
	}
	
	// Interface Implementation in Rectangle Class
	class Rectangle implements Area {
		public float computeArea(float x, float y){
			return ( x * y );
		}
	}
	
	// Interface Implementation in Circle Class
	class Circle implements Area{
		public float computeArea(float x, float y){
			return ( PI * x * y );
		}
	}
	
	class InterfaceImplementation{
		public static void main(String args[]){
			Rectangle myRectangle = new Rectangle();
			Circle myCircle = new Circle();
	
			Area shape;
	
			shape = myRectangle;
			System.out.println("Area of Rectangle = "+shape.computeArea(10, 20));
	
			shape = myCircle;
			System.out.println("Area of Rectangle = "+shape.computeArea(10, 20));
		}
      }

// Output 
   Area of Rectangle = 200.0
   Area of Rectangle = 628.0

हम अलग-अलग प्रकार की विभिन्न Classes में किसी Interface को Implement कर सकते हैं। फिर भी Method को Implement करने के लिए हमें Class के Object को Interface द्वारा Refer करना जरूरी होता है। हम किसी Class के Method से किसी Implemented Method को Execute नहीं कर सकते हैं।

एक बात हमेंशा ध्‍यान रखें कि यदि हम किसी Class में किसी Interface को Implement करते हैं, लेकिन उस Interface की सभी Methods को Class में Implement नहीं करते हैं, तो जिस Class में हम Interface के कुछ Methods को Implement करते हैं, वह Class भी Abstract Class बन जाती है और हम उस Class के भी Object Create नहीं कर सकते हैं।

जब हम किसी Interface को Implement करते हैं, तब Interface को Implement की जा रही Class में Abstract Method को Public Form में Implement करना जरूरी होता है, चाहे हमने Interface में Method को Public Declare किया हो चाहे ना किया हो।

पिछले उदाहरण में हमने Rectangle व Circle दोनों ही Classes में computArea() Method को Public Form में Implement किया है, जबकि Interface Area में computArea() Abstract Method के साथ किसी Access Specifier का प्रयोग नहीं किया है। यदि हम ऐसा नहीं करते हैं, तो जावा Compiler हमें Error देता है।

साथ ही Interface के Abstract Method का Signature व उसे जिस Class में Implement किया जा रहा है, उस Class में Implement किए जा रहे Method] दोनों का Signature समान होना भी जरूरी होता है। वे Classes जिनमें किसी Interface को Implement किया जा रहा होता है, उनमें अन्‍य Non-Interface Methods भी Define किए जा सकते हैं।

हम ऐसे Interface भी Create कर सकते हैं, जिसमें कोई भी Abstract Method ना हो, बल्कि उसमें केवल Constant Variables ही हों, जिन्हें विभिन्न प्रकार की Classes में समान रूप से Use किया जाना हो। यानी यदि कुछ ऐसे Constant मान हों जिनकी जरूरत विभिन्न प्रकार की कई Classes में पडती है, तो हम इस प्रकार के Constant Members को भी Interface मे Specify कर सकते है। फिर जिस किसी भी Class में हमें उन Constant Members की जरूरत होती है, उन Classes में उस Interface को Implement कर लेते हैं।

ये Interfaces वास्तव में “C” की एक Header File के समान ही काम करते हैं, जिसमें विभिन्न प्रकार के #define प्रकार के Constants या const Keyword के साथ Declare किए गए Constants को Store करके रखा जाता है। जब भी हमें किसी Program में इन Constant मानों के समूह में से किसी मान की जरूरत होती है, हम उस Header File को अपने Program में Include करके उन Constants को Use कर लेते हैं, उसी तरह से इस प्रकार के Interfaces को हम उस Class में Implement कर लेते हैं, जिनमें इन Interfaces में Define किए गए Constants की जरूरत होती है।

चूंकि ऐसे Interfaces में कोई Method नहीं होता है, इसलिए हमें Interface के किसी भी Method को Implement करने की जरूरत नहीं होती है। हम Interface के सभी Constants को अपनी Class में Directly Access करके Use कर लेते हैं। इन Interfaces के Constants को हम Class के किसी भी Method में Use कर सकते हैं। इस प्रक्रिया को हम निम्नानुसार Code Segment से समझ सकते हैं:

	interface OnlyConstants
	{
		int xCoordinate = 200;
		int yCoordinate = 250;
	}
	
	class ImplementOnlyConstantsInterface implements OnlyConstants
	{
		int newCoordinate = xCoordinate;
	
		void getNewCoordinate(int center)
		{
			. . .
			. . .
			if(yCoordinate < newCoordinate)
			{
				. . . 
				. . . 
			}
		}
	}

जब हम इस प्रकार का Interface Create करते हैं, तब Interface के Data Members ऐसे Constants होते हैं, जो विभिन्न Classes द्वारा आपस में Share होते हैं। इस प्रकार के Interface को किसी Class में Implement करने का मतलब होता है कि हम Interface के सभी Constants को एक प्रकार से अपनी उस Class के लिए Available कर रहे हैं, ताकि हमारी Class Interface के उन सभी Constants को Use कर सके।

वास्तव में इस प्रकार के Interfaces Classes में किसी भी तरह से Implement नहीं होते हैं, बल्कि उनमें उपलब्ध या Declared सभी Constants हमारी Class में उपलब्ध हो जाते हैं, जिन्हें Class अपनी जरूरत के अनुसार Use कर सकता है।

जब हम एक ऐसी Class Create करते हैं, जिसमें किसी Interface को Implement तो किया जाता है, लेकिन Interface के सभी Methods को Implement नहीं किया जाता है, तब ऐसी Class को हमें Abstract Class Declare करना जरूरी होता है। इस प्रकार के Implementation को Partial Implementation कहते हैं। क्योंकि इस स्थिति में हमारे सामने जो Class होती है, वह भी एक Abstract Class होती है, क्योंकि Extend होने के बाद सभी Interfaces Class में आ जाते हैं।

अब यदि हम Class में Interface से Extend होने वाली सभी Methods को Implement नहीं करते हैं, तो Class में Abstract Method रहते है और जिस Class में Abstract Method रहते हैं, उस Class को Abstract Class Declare करना जरूरी होता है।

Java Programming Language in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Java in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

Java Programming Language in Hindi | Page: 682 | Format: PDF

BUY NOW GET DEMO REVIEWS