इस ट्यूटोरियल में, हम जावा इंटरफेस के बारे में जानेंगे। हम सीखेंगे कि इंटरफेस को कैसे लागू किया जाए और कब उन्हें उदाहरणों की मदद से विस्तार से इस्तेमाल किया जाए।
एक इंटरफ़ेस एक पूरी तरह से अमूर्त वर्ग है जिसमें बिना शरीर के तरीकों का एक समूह शामिल है।
जावा में, एक इंटरफ़ेस विशिष्टताओं के एक सेट को परिभाषित करता है जिसे अन्य वर्गों को लागू करना चाहिए। उदाहरण के लिए,
interface Language ( public void getName(); )
यहां, हमने interface
कीवर्ड का उपयोग भाषा नामक एक इंटरफ़ेस बनाने के लिए किया है । भाषा इंटरफ़ेस एक विनिर्देश को परिभाषित करता है getName()
।
अब, इस इंटरफ़ेस का उपयोग करने वाले प्रत्येक वर्ग को getName()
विनिर्देश लागू करना चाहिए ।
उदाहरण: जावा इंटरफ़ेस
// create an interface interface Language ( void getName(String name); ) // class implements interface class ProgrammingLanguage implements Language ( // implementation of abstract method public void getName(String name) ( System.out.println("Programming Language: " + name); ) ) class Main ( public static void main(String() args) ( ProgrammingLanguage language = new ProgrammingLanguage(); language.getName("Java"); ) )
आउटपुट
प्रोग्रामिंग भाषा: जावा
उपरोक्त उदाहरण में, हमने भाषा नाम का एक इंटरफ़ेस बनाया है। इंटरफ़ेस में एक सार विधि शामिल है getName()
।
यहां, ProgrammingLanguage वर्ग इंटरफ़ेस को लागू करता है और विधि के लिए कार्यान्वयन प्रदान करता है।
abstract
इंटरफ़ेस के अंदर अमूर्त विधियों की घोषणा करते समय कीवर्ड का उपयोग करना अनिवार्य नहीं है। ऐसा इसलिए है क्योंकि एक इंटरफ़ेस में केवल अमूर्त विधियाँ शामिल हैं न कि नियमित विधियाँ।
नोट : एक इंटरफ़ेस के अंदर सभी तरीके निहित हैं public
और सभी फ़ील्ड निहित हैं public static final
। उदाहरण के लिए,
interface Language ( // by default public static final String type = "programming language"; // by default public void getName(); )
एक इंटरफ़ेस लागू करना
अमूर्त कक्षाओं की तरह, हम एक अंतरफलक की वस्तुओं का निर्माण नहीं कर सकते। हालाँकि, हम एक इंटरफ़ेस लागू कर सकते हैं।
हम implements
एक इंटरफ़ेस को लागू करने के लिए कीवर्ड का उपयोग करते हैं । उदाहरण के लिए,
// create an interface interface Polygon ( void getArea(int length, int breadth); ) // implement the Polygon interface class Rectangle implements Polygon ( // implementation of abstract method public void getArea(int length, int breadth) ( System.out.println("The area of the rectangle is " + (length * breadth)); ) ) class Main ( public static void main(String() args) ( // create an object Rectangle r1 = new Rectangle(); r1.getArea(5, 6); ) )
आउटपुट
आयत का क्षेत्रफल 30 है
उपरोक्त उदाहरण में, हमने बहुभुज नाम का एक इंटरफ़ेस बनाया है। इंटरफ़ेस में एक अमूर्त विधि है getArea()
।
यहाँ, आयत वर्ग बहुभुज को लागू करता है। और, getArea()
विधि के कार्यान्वयन प्रदान करता है ।
नोट : एक वर्ग कई इंटरफेस को लागू कर सकता है। उदाहरण के लिए,
interface A ( // members of A ) interface B ( // members of B ) class C implements A, B ( // abstract members of A // abstract members of B )
एक इंटरफ़ेस का विस्तार
कक्षाओं के समान, इंटरफेस अन्य इंटरफेस का विस्तार कर सकते हैं। extends
कीवर्ड इंटरफेस के विस्तार के लिए प्रयोग किया जाता है। उदाहरण के लिए,
interface Line ( // members of Line interface ) // extending interface interface Polygon extends Line ( // members of Polygon interface // members of Line interface )
यहां, बहुभुज इंटरफ़ेस लाइन इंटरफ़ेस का विस्तार करता है। अब, यदि कोई वर्ग बहुभुज को लागू करता है, तो उसे रेखा और बहुभुज दोनों के सभी अमूर्त तरीकों के लिए कार्यान्वयन प्रदान करना चाहिए।
नोट : एक इंटरफ़ेस कई इंटरफेस बढ़ा सकता है। उदाहरण के लिए,
interface A (… ) interface B (… ) interface C extends A, B (… )
जावा में इंटरफ़ेस के लाभ
अब जब हम जानते हैं कि इंटरफेस क्या हैं, तो आइए जानें कि जावा में इंटरफेस का उपयोग क्यों किया जाता है।
- इंटरफेस उन विशिष्टताओं को प्रदान करते हैं जो एक वर्ग (जो इसे लागू करता है) का पालन करना चाहिए।
हमारे पिछले उदाहरण में, हमनेgetArea()
इंटरफ़ेस बहुभुज के अंदर एक विनिर्देश के रूप में उपयोग किया है। यह एक नियम स्थापित करने जैसा है कि, हमें हर बहुभुज का क्षेत्रफल प्राप्त करने में सक्षम होना चाहिए।
अब कोई भी वर्ग जो बहुभुज इंटरफ़ेस को लागू करता है, उसेgetArea()
विधि के लिए एक कार्यान्वयन प्रदान करना होगा । - अमूर्त कक्षाओं के समान, इंटरफेस जावा में अमूर्तता प्राप्त करने में हमारी मदद करते हैं ।
यहाँ, हम जानते हैं किgetArea()
बहुभुज के क्षेत्रफल की गणना की जाती है लेकिन जिस तरह से गणना की जाती है वह भिन्न बहुभुज के लिए भिन्न होती है। इसलिए, कार्यान्वयनgetArea()
एक दूसरे से स्वतंत्र है। - जावा में कई विरासत प्राप्त करने के लिए इंटरफेस का भी उपयोग किया जाता है। उदाहरण के लिए,
interface Line (… ) interface Polygon (… ) class Rectangle implements Line, Polygon (… )
यहां, वर्ग आयत दो अलग-अलग इंटरफेस को लागू कर रहा है। इस तरह हम जावा में कई उत्तराधिकार प्राप्त करते हैं।
जावा इंटरफेस में डिफ़ॉल्ट तरीके
जावा 8 की रिलीज के साथ, हम अब एक इंटरफ़ेस के अंदर कार्यान्वयन के साथ तरीके जोड़ सकते हैं। इन विधियों को डिफ़ॉल्ट विधियाँ कहा जाता है।
इंटरफेस के अंदर डिफ़ॉल्ट तरीकों की घोषणा करने के लिए, हम default
कीवर्ड का उपयोग करते हैं । उदाहरण के लिए,
public default void getSides() ( // body of getSides() )
डिफ़ॉल्ट तरीके क्यों?
Let's take a scenario to understand why default methods are introduced in Java.
Suppose, we need to add a new method in an interface.
We can add the method in our interface easily without implementation. However, that's not the end of the story. All our classes that implement that interface must provide an implementation for the method.
If a large number of classes were implementing this interface, we need to track all these classes and make changes in them. This is not only tedious but error-prone as well.
To resolve this, Java introduced default methods. Default methods are inherited like ordinary methods.
Let's take an example to have a better understanding of default methods.
Example: Default Method in Java Interface
interface Polygon ( void getArea(); // default method default void getSides() ( System.out.println("I can get sides of a polygon."); ) ) // implements the interface class Rectangle implements Polygon ( public void getArea() ( int length = 6; int breadth = 5; int area = length * breadth; System.out.println("The area of the rectangle is " + area); ) // overrides the getSides() public void getSides() ( System.out.println("I have 4 sides."); ) ) // implements the interface class Square implements Polygon ( public void getArea() ( int length = 5; int area = length * length; System.out.println("The area of the square is " + area); ) ) class Main ( public static void main(String() args) ( // create an object of Rectangle Rectangle r1 = new Rectangle(); r1.getArea(); r1.getSides(); // create an object of Square Square s1 = new Square(); s1.getArea(); s1.getSides(); ) )
Output
The area of the rectangle is 30 I have 4 sides. The area of the square is 25 I can get sides of a polygon.
In the above example, we have created an interface named Polygon. It has a default method getSides()
and an abstract method getArea()
.
Here, we have created two classes Rectangle and Square that implement Polygon.
The Rectangle class provides the implementation of the getArea()
method and overrides the getSides()
method. However, the Square class only provides the implementation of the getArea()
method.
Now, while calling the getSides()
method using the Rectangle object, the overridden method is called. However, in the case of the Square object, the default method is called.
private and static Methods in Interface
The Java 8 also added another feature to include static methods inside an interface.
Similar to a class, we can access static methods of an interface using its references. For example,
// create an interface interface Polygon ( staticMethod()(… ) ) // access static method Polygon.staticMethod();
Note: With the release of Java 9, private methods are also supported in interfaces.
We cannot create objects of an interface. Hence, private methods are used as helper methods that provide support to other methods in interfaces.
Practical Example of Interface
Let's see a more practical example of Java Interface.
// To use the sqrt function import java.lang.Math; interface Polygon ( void getArea(); // calculate the perimeter of a Polygon default void getPerimeter(int… sides) ( int perimeter = 0; for (int side: sides) ( perimeter += side; ) System.out.println("Perimeter: " + perimeter); ) ) class Triangle implements Polygon ( private int a, b, c; private double s, area; // initializing sides of a triangle Triangle(int a, int b, int c) ( this.a = a; this.b = b; this.c = c; s = 0; ) // calculate the area of a triangle public void getArea() ( s = (double) (a + b + c)/2; area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); System.out.println("Area: " + area); ) ) class Main ( public static void main(String() args) ( Triangle t1 = new Triangle(2, 3, 4); // calls the method of the Triangle class t1.getArea(); // calls the method of Polygon t1.getPerimeter(2, 3, 4); ) )
Output
Area: 2.9047375096555625 Perimeter: 9
In the above program, we have created an interface named Polygon. It includes a default method getPerimeter()
and an abstract method getArea()
.
We can calculate the perimeter of all polygons in the same manner so we implemented the body of getPerimeter()
in Polygon.
Now, all polygons that implement Polygon can use getPerimeter()
to calculate perimeter.
हालांकि, क्षेत्र की गणना के लिए नियम अलग-अलग बहुभुज के लिए अलग है। इसलिए, getArea()
कार्यान्वयन के बिना शामिल है।
बहुभुज को लागू करने वाले किसी भी वर्ग को इसका कार्यान्वयन प्रदान करना चाहिए getArea()
।