Java Interface In Hindi

📔 : Java 🔗

Java में Interface , Abstraction achieve करने का दूसरा तरीका है, Interface का use करके 100% Abstraction achieve की जा सकती है। Interface में defined सभी methods abstract होते हैं इसलिए जो भी class इसको implement करेगी उस class को सभी methods का implementation provide करना पड़ेगा।

Interface को interface keyword के साथ define किया जाता है , और Inherit करने के लिए implements keyword का use किया जाता है। interface में सिर्फ variables और abstract methods define कर सकते हैं।

Java 8 अब किसी interface में default और static methods भी define कर सकते हैं और Java 9 से private methods भी define कर सकते हैं।

Why Use Interfaces

Interface को तीन वजह से use करते हैं -

  1. To enhance security : किसी functionality की internal details hide करना और सिर्फ working parts को ही show करना ।

  2. Multiple inheritance : हम जानते हैं कि Java में आप एक से ज्यादा class को inherit नहीं कर सकते हैं। लेकिन आप एक से ज्यादा Interface को implement कर सकते हैं।

  3. To achieve 100% Abstraction.

Java Interface Example

File : MainClass.java

CopyFullscreenClose FullscreenRun
interface Animal{  
  String message = "Animal Message";
  public abstract void eat();
}  

// now implement interface.
class Horse implements Animal {
  // provide implementation.
  public void eat() {
    System.out.println("Horse eats grass.");
  } 
}

// main class.
public class MainClass {
  public static void main(String args[]){  
    Horse h = new Horse(); // create Horse Object.
    h.eat();
    System.out.println(h.message);
  }
}
Output
javac MainClass.java
java MainClass
Bike is running

example में आप देख सकते हैं कि किस तरह से Interface के abstract methods का implementation provide किया गया है। Interface को use करते समय नीचे दिए गए points हमेशा ध्यान में रखें।

Important Points About Interface

  • Abstract Class की तरह Interface का Object नहीं बना सकते हैं। और न ही constructor define कर सकते हैं।

  • जो class Interface में defined सभी abstract methods का implementation provide कराना mandatory है।

  • Interface में define किये जाने वाले में से कोई भी abstract method private नहीं होना चाहिए , या तो default हो या public . और implement करते time सभी method public होना ही चाहिए। Otherwise error आएगी।

  • चूंकि किसी भी interface का object create नहीं होता है और न ही constructor होता है , तो आप एक साथ एक से ज्यादा Interfaces को implement कर सकते हैं।

A class can implement more than one Interface at a time

File : MainClass.java

CopyFullscreenClose FullscreenRun
interface FirstInerface{  
  public abstract void methodOne();
}  

// define another interface.
interface SecondInerface{  
  public abstract void methodOther();
}  

// implement both Interfaces.
class Test implements FirstInerface, SecondInerface {
	public void methodOne() {
      System.out.println("methodOne of FirstInerface.");
    }
    
    public void methodOther() {
      System.out.println("methodOther of SecondInerface.");
    }
}

// main class.
public class MainClass {
  public static void main(String args[]){  
    Test t = new Test();
    t.methodOne();
    t.methodOther();
  }
}
Output
javac MainClass.java
java MainClass
methodOne of FirstInerface.
methodOther of SecondInerface.
Why a class can implement more than on Interface ?

हम जानते हैं कि Inheritance में parent class के constructor run करने के लिए super() का use किया जाता है। अगर एक से ज्यादा class को inherit करते हैं तो super() call करने पर ambiguity create होगी कि किसी parent class का constructor call हो। लेकिन एक single class दूसरी class को inherit करने के साथ साथ एक से ज्यादा Interfaces को implement कर सकती है , इसका सबसे बड़ा reason यही है , क्योंकि Interface में constructor define नहीं कर सकते हैं इसलिए super() को आसानी से use कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers