If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
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 कर सकते हैं।
Interface को तीन वजह से use करते हैं -
To enhance security : किसी functionality की internal details hide करना और सिर्फ working parts को ही show करना ।
Multiple inheritance : हम जानते हैं कि Java में आप एक से ज्यादा class को inherit नहीं कर सकते हैं। लेकिन आप एक से ज्यादा Interface को implement कर सकते हैं।
To achieve 100% Abstraction.
File : MainClass.java
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);
}
}
javac MainClass.java
java MainClass
Bike is running
example में आप देख सकते हैं कि किस तरह से Interface के abstract methods का implementation provide किया गया है। Interface को use करते समय नीचे दिए गए points हमेशा ध्यान में रखें।
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 कर सकते हैं।
File : MainClass.java
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();
}
}
javac MainClass.java
java MainClass
methodOne of FirstInerface.
methodOther of SecondInerface.
हम जानते हैं कि 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 कर सकते हैं।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook