Interfaces & Inheritance

Inherits एक parent लेता है, Implements कई contracts लेता है, और VB वह paperwork जोड़ता है जो Java ने skip कर दिया था: हर implementing member को अपने interface method का नाम एक explicit Implements clause से लेना ज़रूरी है।

11 min read · 9 cards · 2 checks

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


Theory

Receipt की Story, फिर से

Unit 4 का अंत, और ShopKeeper उसी exact dilemma का सामना करता है जो BookBridge ने BCA403 में किया था: receipts, day reports और price tags सबको printable होना चाहिए, पर उनका कोई sensible parent share नहीं होता, और Inherits, extends की तरह, एक-parent वाला मामला है।

आप जानते हैं answer एक interface है: एक contract, कोई bloodline नहीं। VB.NET सहमत है, फिर अपना एक rule जोड़ता है: contract sign करना काफ़ी नहीं है; हर method को writing में बताना ज़रूरी है यह contract की कौन सी clause पूरी करता है।

Theory

Inherits और Implements, Formally

Inherits class-to-class है: Class Cashier : Inherits Person। Exactly ONE parent, members पिछले lesson वाले Overridable/Overrides pair से override होते हैं।

Interface pure signatures declare करता है (convention: names I से शुरू होते हैं):

Interface IPrintable

Sub PrintReceipt()

End Interface

Implements class-to-contract है, और plural welcome है:

Class BillReceipt

Implements IPrintable, IScannable

और interfaces खुद Inherits से interfaces extend कर सकते हैं।

Practical

दो Contracts, Explicit Wiring

Public Interface IPrintable
    Sub PrintReceipt()
End Interface

Public Interface IScannable
    Function Barcode() As String
End Interface

Public Class BillReceipt
    Implements IPrintable, IScannable

    Public Sub PrintReceipt() Implements IPrintable.PrintReceipt
        MsgBox("Receipt printed")
    End Sub

    Public Function Barcode() As String Implements IScannable.Barcode
        Return "8901234567890"
    End Function
End Class

' Cross-family polymorphism, no bloodline needed:
' Dim p As IPrintable = New BillReceipt()
' p.PrintReceipt()

Theory

वह Clause जो Java ने कभी नहीं माँगी

Method वाली line फिर से देखिए:

Public Sub PrintReceipt() Implements IPrintable.PrintReceipt

Java में, एक matching name और signature चुपचाप interface को satisfy कर देता था। VB.NET में trailing Implements clause compulsory है: इसके बिना, method बस एक method है, contract unfulfilled रहता है, और class compile होने में fail हो जाती है।

यह paperwork 2 असली abilities खरीदता है: method interface member से अलग नाम इस्तेमाल कर सकता है (clause ही link है, name नहीं), और जब 2 interfaces same-named member माँगें, अलग methods हर एक अपने master को unambiguously serve कर सकते हैं।

Quiz

BillReceipt Implements IPrintable declare करता है, और सही signature वाला Public Sub PrintReceipt() रखता है पर कोई trailing Implements clause नहीं। क्या होता है?

  1. यह compile होता है: matching name और signature interface को satisfy करते हैं, Java की तरह
  2. Compile error: interface member unimplemented है; clause ही एकमात्र चीज़ है जो इसे fulfil करती है
  3. यह compile होता है, पर एक IPrintable reference से call करने पर run time पर throw होता है
  4. यह एक warning के साथ compile होता है और VB method को automatically wire कर देता है
Show the answer

Compile error: interface member unimplemented है; clause ही एकमात्र चीज़ है जो इसे fulfil करती है

VB.NET implementations को interface members से SIRF explicit clause के ज़रिए link करता है: बिना इसके एक same-named method एक coincidence है, fulfilment नहीं, तो class को IPrintable.PrintReceipt implement न करने वाली report किया जाता है। Option A वह Java reflex है जिसे catch करने के लिए यह question exist करता है, दोनों languages के interfaces के बीच सबसे sharp single difference। Options C और D एक hard rule को soften करते हैं: कुछ भी run time पर deferred नहीं है और कुछ भी auto-wired नहीं है। बनाने लायक habit: Implements IPrintable type कीजिए, Enter press कीजिए, और IDE को correctly-claused stubs generate करने दीजिए।

Think first

आख़िरी बार, Tool चुनिए

ShopKeeper की तीन needs: (1) Cashier एक Person HAI और इसके fields reuse करता है, (2) receipts, reports AND price tags सबको PrintReceipt() promise करना ज़रूरी है, (3) एक future IEmailable को भी IPrintable plus कुछ और count होना चाहिए। Tap करने से पहले Inherits और Implements assign कीजिए।

Show the answer

(1) Inherits Person: shared state के साथ genuine is-a; एक parent ही सब कुछ है जो आपको मिलता है और चाहिए। (2) हर unrelated class पर Implements IPrintable: families के आर-पार एक shared promise, exactly BCA403 का Printable pattern। (3) Interface IEmailable : Inherits IPrintable: interfaces interfaces को extend करते हैं, contracts stack करते हुए। वह compact rule जो हर language में जीवित रहता है जिससे आप मिले हैं: वह inherit कीजिए जो आप HAIN, वह implement कीजिए जो आप PROMISE करते हैं।

Watch out

Interface की Fine Print

कोई instantiation नहीं: New IPrintable() meaningless और illegal है; interface variables implementing objects HOLD करते हैं।

सब कुछ implement कीजिए: एक member (या इसकी clause) miss कीजिए और class को MustInherit बनना ही पड़ेगा वरना यह compile नहीं होगी।

Keyword swap alert: class extends interface Java की grammar है; VB में एक class एक interface IMPLEMENTS करती है, एक interface एक interface INHERITS करता है, एक class एक class INHERITS करती है। तीन sentences, तीन keywords, drop करने के लिए easy marks।

Theory

Unit 4, OOP Circle बंद करती है

गिनिए ShopKeeper की classes अब क्या करती हैं: guarded Properties (encapsulation), GST polymorphism वाली एक MustInherit Product family, Me/MyBase/MyClass navigate करती staff classes, Private से Protected Friend तक access doors, और honest paperwork वाले interfaces से contracts। यही पूरा OOP surface है जिसे यह paper examine करता है। Unit 5 इसे वहाँ काम में लाती है जहाँ shops जीते या मरते हैं: bills को एक असली database में ADO.NET से save करना, जहाँ Unit 3 का DataSet आख़िरकार अपने DataAdapter से मिलता है।

Summary

Key takeaways

  • Inherits: एक parent class, is-a, shared state, behaviour के लिए Overridable/Overrides।
  • Interface ... End Interface signatures declare करता है; names conventionally I से शुरू होते हैं।
  • एक class कई interfaces Implements करती है; interfaces दूसरे interfaces Inherits करते हैं।
  • हर implementing member को explicit clause चाहिए: Sub X() Implements IFoo.X: सिर्फ़ name-matching कुछ नहीं है।
  • Clause renamed implementations allow करता है और 2 contracts से same-named members resolve करता है।
  • Interface references unrelated families के आर-पार polymorphism देते हैं: Dim p As IPrintable = New BillReceipt()।
  • Memory hook: जो आप हैं वह inherit कीजिए, जो promise करते हैं वह implement कीजिए, और VB में, इसे writing में कहिए।

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 Object Oriented Programming

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

Interfaces & Inheritance · .NET Programming · Gri-Learn