📜
OOP

Interface vs Abstract Class

Contract vs Partial House
💡 Interface एक 100% contract है — सिर्फ़ rules, कोई काम पहले से नहीं किया गया। Abstract class एक अधूरा (half-built) घर है — कुछ काम already हो चुका, बाक़ी तुम्हे करना है।

Interface में (Java 8 से पहले) सिर्फ़ method signatures होते थे, कोई body नहीं — implementing class को सब लिखना पड़ता था।

Abstract class में कुछ methods complete हो सकते हैं और कुछ abstract (incomplete) — और एक class सिर्फ़ एक abstract class extend कर सकती है, पर कई interfaces implement कर सकती है।

interface Flyable { void fly(); }

abstract class Bird {
  void breathe() { System.out.println("breathing"); }
  abstract void fly();
}
📜
Interface एक 100% contract है — सिर्फ़ rules, कोई काम पहले से नहीं किया गया। Abstract class एक अधूरा (half-built) घर है — कुछ काम already हो चुका, बाक़ी तुम्हे करना है।
1 / 4
⚡ झट से Recap
  • Interface = 100% contract, multiple allowed
  • Abstract class = partial, सिर्फ़ एक allowed
  • Java 8+ interface में default methods भी
इस page में (5 subtopics)

एक class multiple interfaces implement कर सकती है (comma से separate करके) — ये Java में "multiple inheritance of type" का safe तरीक़ा है (classes के साथ जो allowed नहीं वो interfaces के साथ allowed है)। जैसे एक class Flyable और Swimmable दोनों implement कर सकती है (जैसे Duck class, जो दोनों काम कर सकती है)।

interface Flyable { void fly(); }
interface Swimmable { void swim(); }
class Duck implements Flyable, Swimmable {
  public void fly() { System.out.println("Flying"); }
  public void swim() { System.out.println("Swimming"); }
}

Java 8 से पहले interface में सिर्फ़ abstract methods होते थे — 100% contract। अब default methods (body के साथ) add कर सकते हो — existing interfaces को नए methods से extend करना आसान होता है बिना सारी implementing classes तोड़े (जैसे List interface में sort() method Java 8 में add हुआ, पुरानी classes automatically काम करती रहीं)।

static methods भी हो सकते हैं, जो interface के नाम से directly call होते हैं (Interface.method()) — implementing class के through नहीं।

interface Vehicle {
  void drive();
  default void honk() { System.out.println("Beep!"); } // default body
}

एक interface जिसमें सिर्फ़ एक ही abstract method हो उसे "Functional Interface" कहते हैं (default/static methods count नहीं होते इसमें)। ये Lambda expressions के target type होते हैं — Java 8+ का core concept (Java 8+ topic में पूरा detail है)।

@FunctionalInterface annotation optional है लेकिन best practice — compiler check करता है कि interface में सच में सिर्फ़ एक abstract method है, वरना compile error देता है (अगर ग़लती से दूसरा abstract method add हो जाए)।

कुछ interfaces बिल्कुल खाली होते हैं, कोई method ही नहीं — जैसे Serializable, Cloneable। ये सिर्फ़ एक "marker/tag" की तरह काम करते हैं, JVM को बताते हैं कि इस class के objects को एक special तरीक़े से treat करना है (जैसे Serializable objects को byte stream में convert किया जा सकता है, non-Serializable को नहीं)।

ये interviews का सबसे common comparison है — दोनों का purpose overlap करता है लेकिन key differences हैं जो decide करते हैं कब कौनसा use करना है।

CriteriaInterfaceAbstract Class
MethodsAbstract + default/static (Java 8+)Abstract + concrete दोनों
Multiple inheritanceClass कई implement कर सकती हैसिर्फ़ एक extend कर सकती है
Constructorsनहीं हो सकतेहो सकते हैं
Fieldsसिर्फ़ public static final (constants)किसी भी type के instance fields
कब use करेंUnrelated classes में common contractRelated classes में shared code