Interfaces: introduction and declaration; inheriting and hiding concepts; inheriting, overloading and overriding methods and constructors; interface implementations

एक interface एक pure contract है: कोई bodies नहीं, कोई constructors नहीं, implicitly public abstract methods, और एक class एक साथ कई implement कर सकती है, जो multiple inheritance का Java का answer है।

12 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

तीन चीज़ें जिन्हें Print होना है

BookBridge अब issued books, fine payments और नए membership cards के receipts print करता है। तीन classes, family से unrelated: एक FineReceipt एक MembershipCard का kind नहीं है।

फिर भी dashboard इन्हें identically treat करना चाहता है: "सब कुछ printable, closing time पर खुद को print कीजिए।"

Inheritance यह express नहीं कर सकता: कोई sensible common parent नहीं है, और Java anyway सिर्फ़ एक extends allow करता है। हमें जो चाहिए वह एक shared ancestor नहीं बल्कि एक shared promise है: यहाँ sign कीजिए, और आप print कर सकते हैं।

Theory

एक Contract, Bloodline नहीं

एक driving licence परवाह नहीं करता आपके parents कौन हैं: test pass कीजिए, और आप drive कर सकते हैं। Doctors, students और shopkeepers सब एक रखते हैं।

एक interface classes के लिए वह licence है: कोई implementation attached न होने वाली abilities की एक list। किसी भी family की कोई भी class इसे sign कर सकती है, और sign करने का मतलब एक चीज़ है: आपको असल में ये methods provide करने होंगे। Family inherited होती है; contracts sign होते हैं। Java एक class को एक family और कई contracts रखने देता है।

Theory

Declaration और Implicit Rules

interface Printable {

void print();

}

Compiler silently क्या add करता है, और exams क्या पूछते हैं:

  • हर method implicitly public abstract है: एक signature, कोई body नहीं
  • हर field implicitly public static final है: एक constant, initialise होना चाहिए
  • कोई constructors नहीं हैं: new Printable() meaningless और illegal है

एक class implements से sign करती है और सारे methods define करने चाहिए, या खुद को abstract declare करना चाहिए:

class FineReceipt implements Printable { ... }

Practical

दो Contracts, एक Class

interface Printable {
    void print();                      // implicitly public abstract
}

interface Payable {
    void collect(int amount);
}

class FineReceipt implements Printable, Payable {   // many contracts at once
    public void print() {              // public is COMPULSORY here
        System.out.println("Fine receipt printed");
    }
    public void collect(int amount) {
        System.out.println("Collected Rs " + amount);
    }
}

public class BookBridge {
    public static void main(String[] args) {
        Printable p = new FineReceipt();   // interface reference
        p.print();
    }
}

Theory

Multiple-Inheritance का Debt, चुकाया गया

Java-बनाम-C++ lesson में, एक promise किया गया था: Java 2 parent classes से मना करता है पर idea तक एक और route है। यह वही है।

class FineReceipt implements Printable, Payable दोनों contracts लेता है, और interfaces खुद inherit कर सकते हैं: interface Printable extends Displayable contract के ऊपर contract stack करता है।

यह safe क्यों है जब 2 parent classes नहीं थीं? Interfaces में कोई state और कोई method bodies नहीं होतीं, तो C++ का diamond problem (किस parent का data? किसका implementation?) के पास collide करने के लिए कुछ नहीं है।

Quiz

class Card implements Printable print() declare करता है पर public keyword भूल जाता है: void print() { ... } क्या होता है?

  1. Fine compile होता है; package के अंदर default access काफ़ी है
  2. Compile error: एक implementing method public से weaker access नहीं रख सकता
  3. जब print() पहली बार interface के ज़रिए call होता है तब runtime error
  4. Compiler silently आपके लिए public add कर देता है
Show the answer

Compile error: एक implementing method public से weaker access नहीं रख सकता

Interface methods implicitly public हैं, और एक override कभी visibility REDUCE नहीं कर सकता, तो default access एक step down है और javac इसे reject करता है: यह single सबसे common compile error है जब students पहली बार interfaces implement करते हैं। Option A एक context से package rule apply करता है जो इसे govern नहीं करता। Option C बहुत देर से है: Java compile time पर access settle कर देता है। Option D describe करता है interfaces अपनी खुद की declarations के साथ क्या करते हैं, आपकी implementing class के साथ नहीं।

Think first

Interface बनाम Abstract Class

दोनों abstract methods hold कर सकते हैं, दोनों instantiate नहीं हो सकते। Tap करने से पहले: 2 differences बताइए जो decide करते हैं आप एक interface कब choose करेंगे।

Show the answer

1: कितने। एक class ज़्यादा से ज़्यादा 1 abstract class extend करती है पर कई interfaces implement करती है; अगर एक Card को Printable और Payable दोनों होना है, interfaces ही एकमात्र route हैं। 2: वे क्या contain कर सकते हैं। एक abstract class में instance fields, constructors और children के inherit करने के लिए ordinary method bodies हो सकते हैं; एक interface में इनमें से कुछ नहीं होता, सिर्फ़ contract (इसके fields public static final constants हैं)। Rule of thumb: shared implementation, abstract class; shared capability, interface।

Watch out

3 Interface Traps

new Printable() compile नहीं होता: कोई constructors नहीं, build करने के लिए कुछ नहीं। एक interface variable सिर्फ़ एक implementing object HOLD कर सकता है: Printable p = new FineReceipt();

Partial Implementation: 3 में से 2 methods implement कीजिए और class को abstract declared होना चाहिए, वरना यह compile नहीं होगा।

extends बनाम implements: class से class extends है, class से interface implements है, interface से interface extends है। इन्हें swap करना एक instant compile error है।

Theory

यह Pattern Java World चलाता है

Interface references families के आर-पार polymorphism देते हैं: dashboard Printable references hold करता है और print() call करता है बिना यह परवाह किए हर object असल में क्या है, exactly Person p = new Member() जैसा पर कोई bloodline required नहीं। Shape mind में रखिए: Unit 4 में, BookBridge के due-date reminder को background में run कराना literally class Reminder implements Runnable है, एक और signed contract। Android (BCA305-02) ने हर button click को same तरीके से wire किया।

Summary

Key takeaways

  • एक interface एक pure contract है: method signatures, कोई bodies नहीं, कोई state नहीं, कोई constructors नहीं।
  • Methods implicitly public abstract हैं; fields implicitly public static final हैं।
  • एक class कई interfaces implement करती है और हर method को public define करना चाहिए, या abstract होना चाहिए।
  • Interfaces interfaces को extend करते हैं; classes interfaces को implement करती हैं; classes एक class को extend करती हैं।
  • Interface references (Printable p = new FineReceipt()) cross-family polymorphism देते हैं।
  • कोई state नहीं और कोई bodies नहीं यही वजह है multiple contracts safe हैं जहाँ multiple parents नहीं थे।
  • Memory hook: एक family, कई licences।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Classes and Objects

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Interfaces: introduction and declaration; inheriting and hiding concepts; inheriting, overloading and overriding methods and constructors; interface implementations · Java Programming Language · Gri-Learn