← Back to Core Java Course | Chapter 9: OOP Advanced | Lesson 4 of 8

Java के Interfaces

एक interface promises की एक list है जो एक class को रखनी चाहिए, एक job description जैसा। कोई भी class जो sign up करती है उसे वे सभी abilities देनी चाहिए।
Syntax
java
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?

java
interface Drivable {
	void drive(); // contract: what, not how
}
public class Main {
	public static void main(String[] args) {
		System.out.println("Interface declared");
	}
}

Implementing Interfaces

एक class implements keyword से एक interface के contract में opt in करती है, और compiler enforce करता है कि class interface declare करता हर abstract method के लिए एक concrete implementation प्रदान करे, नहीं तो class compile नहीं होगी।

उदाहरण: Implementing Interfaces

java
// 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();
	}
}

Interface Variables

किसी interface के अंदर declared कोई भी field implicitly public static final है, चाहे आप वे modifiers लिखें या नहीं, जो effectively interface fields को per-instance state के बजाय shared, unchangeable, global constants बनाता है।

उदाहरण: Interface Variables

java
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);
	}
}

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

java
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();
	}
}

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

java
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
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. सभी interface methods implement करना भूल जाना, जो एक compile error है जब तक class abstract न हो।
  2. बिना public के एक interface method implement करना, जो एक compile error है क्योंकि interface methods public हैं।
  3. एक interface variable बदलने की कोशिश करना, जो एक compile error है क्योंकि वे constants हैं।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.