Java के Interfaces
In this page:
interface InterfaceName {
returnType methodName();
}
class ClassName implements InterfaceName {
public returnType methodName() { }
}
What is an Interface?
एक interface एक pure contract define करता है — historically सिर्फ abstract method signatures और constants — describe करते हुए कि एक class क्या करने में सक्षम होनी चाहिए, इसके बारे में कुछ कहे बिना कि यह internally कैसे करती है।
उदाहरण: What is an Interface?
interface Drivable {
void drive(); // contract: what, not how
}
public class Main {
public static void main(String[] args) {
System.out.println("Interface declared");
}
}
Login to try C/C++/Java/PHP code in the editor
Implementing Interfaces
एक class implements keyword से एक interface के contract में opt in करती है, और compiler enforce करता है कि class interface declare करता हर abstract method के लिए एक concrete implementation प्रदान करे, नहीं तो class compile नहीं होगी।
उदाहरण: Implementing Interfaces
// Define the interface Drivable
interface Drivable {
void drive();
}
// Define the class Car
class Car implements Drivable {
public void drive() {
// Print a line to the console
System.out.println("Driving");
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
new Car().drive();
}
}
Login to try C/C++/Java/PHP code in the editor
Interface Variables
किसी interface के अंदर declared कोई भी field implicitly public static final है, चाहे आप वे modifiers लिखें या नहीं, जो effectively interface fields को per-instance state के बजाय shared, unchangeable, global constants बनाता है।
उदाहरण: Interface Variables
interface Constants {
int MAX_SPEED = 120; // implicitly public static final
}
public class Main {
public static void main(String[] args) {
System.out.println(Constants.MAX_SPEED);
}
}
Login to try C/C++/Java/PHP code in the editor
Default Methods in Interfaces
Java 8 ने default methods introduce किए, जो एक interface को सीधे interface में ही एक पूरी तरह implemented method body include करने देते हैं — इसने existing interfaces में नए methods जोड़ना possible बनाया बिना उन्हें पहले से implement करने वाली हर class को तोड़े।
उदाहरण: Default Methods in Interfaces
interface Greeter {
default void greet() { // implemented directly in the interface
System.out.println("Hello!");
}
}
class Person implements Greeter {
}
public class Main {
public static void main(String[] args) {
new Person().greet();
}
}
Login to try C/C++/Java/PHP code in the editor
Static Methods in Interfaces
Interfaces static methods भी define कर सकते हैं, एक implementing object के बजाय सीधे interface name के through callable, जो utility helpers के लिए उपयोगी है जो logically interface के साथ belong करते हैं लेकिन किसी particular implementation की state पर निर्भर नहीं करते।
उदाहरण: Static Methods in Interfaces
interface MathUtils {
static int square(int n) {
return n * n;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.square(5)); // called through interface name
}
}
Login to try C/C++/Java/PHP code in the editor
- सभी interface methods implement करना भूल जाना, जो एक compile error है जब तक class abstract न हो।
- बिना
publicके एक interface method implement करना, जो एक compile error है क्योंकि interface methods public हैं। - एक interface variable बदलने की कोशिश करना, जो एक compile error है क्योंकि वे constants हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: