Interfaces & Inheritance

Inherits એ એક parent લે છે, Implements ઘણા contracts લે છે, અને VB એ paperwork add કરે છે જે Java skip કર્યું: દરેક implementing member ને તેના interface method ને explicit Implements clause સાથે name કરવું જોઈએ.

11 min read · 9 cards · 2 checks

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


Theory

receipt story, retold

Unit 4 નો અંત, અને ShopKeeper એ exactly dilemma ને face કરે છે જે BookBridge એ BCA403 માં face કર્યું હતું: receipts, day reports અને price tags બધા printable હોવા જોઈએ, પણ તેઓ કોઈ sensible parent ને share નથી કરતા, અને Inherits, extends જેવું, એ one-parent affair છે.

તમે જાણો છો કે answer એ interface છે: contract, bloodline નહીં. VB.NET agree કરે છે, પછી તેનો own rule add કરે છે: contract ને sign કરવું enough નથી; દરેક method એ કહેવું જોઈએ કે તે contract ના which clause ને fulfill કરે છે, writing માં.

Theory

Inherits અને Implements, ઔપચારિક રીતે

Inherits એ class-to-class છે: Class Cashier : Inherits Person. exactly ONE parent, members overridden last lesson ના Overridable/Overrides pair દ્વારા.

Interface pure signatures ને declare કરે છે (convention: names I થી start થાય છે):

Interface IPrintable

Sub PrintReceipt()

End Interface

Implements એ class-to-contract છે, અને plural welcome છે:

Class BillReceipt

Implements IPrintable, IScannable

અને interfaces themselves પણ 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, કોઈ bloodline needed નહીં:
' Dim p As IPrintable = New BillReceipt()
' p.PrintReceipt()

Theory

clause જે Java ક્યારેય asked નહોતું

method line ને ફરી look કરો:

Public Sub PrintReceipt() Implements IPrintable.PrintReceipt

Java માં, matching name અને signature silently interface ને satisfy કરતા હતા. VB.NET માં trailing Implements clause compulsory છે: તેના વગર, method એ just method છે, contract unfulfilled stands કરે છે, અને class compile થવામાં fail થાય છે.

paperwork 2 real abilities ને buy કરે છે: method interface member થી different name વાપરી શકે છે (clause એ link છે, name નહીં), અને જ્યારે 2 interfaces same-named member ને demand કરે છે, separate methods દરેક તેના own master ને unambiguously serve કરી શકે છે.

Quiz

BillReceipt એ Implements IPrintable declare કરે છે, અને તેમાં Public Sub PrintReceipt() છે જે right signature ધરાવે છે પણ NO trailing Implements clause નથી. શું થાય છે?

  1. તે compile થાય છે: matching name અને signature interface ને satisfy કરે છે, Java જેવું
  2. Compile error: interface member unimplemented છે; clause એ એકમાત્ર thing છે જે તેને fulfils કરે છે
  3. તે compile થાય છે, પણ IPrintable reference દ્વારા calling run time પર throws કરે છે
  4. તે warning સાથે compile થાય છે અને VB method ને automatically wire કરે છે
Show the answer

Compile error: interface member unimplemented છે; clause એ એકમાત્ર thing છે જે તેને fulfils કરે છે

VB.NET implementations ને interface members સાથે ONLY explicit clause દ્વારા link કરે છે: same-named method તેના વગર એ coincidence છે, fulfilment નહીં, એટલે class ને reported થાય છે કે તે IPrintable.PrintReceipt ને implement નથી કરતું. Option A એ Java reflex છે જે આ question catch કરવા માટે exists કરે છે, 2 languages ના interfaces વચ્ચે નો sharpest single difference. Options C અને D hard rule ને soften કરે છે: કંઈ run time પર deferred નથી અને કંઈ auto-wired નથી. build કરવાની habit: Implements IPrintable type કરો, Enter દબાવો, અને IDE ને correctly-claused stubs ને generate કરવા દો.

Think first

tool ને choose કરો, one last time

ત્રણ ShopKeeper needs: (1) Cashier IS Person છે અને તેના fields ને reuse કરે છે, (2) receipts, reports AND price tags બધા PrintReceipt() ને promise કરવા જોઈએ, (3) future IEmailable એ પણ IPrintable ગણાવું જોઈએ plus more. Inherits અને Implements ને assign કરો tap કરતા પહેલા.

Show the answer

(1) Inherits Person: true is-a shared state સાથે; one parent એ all છે જે તમે get કરો છો અને all જે તમે need કરો છો. (2) Implements IPrintable દરેક unrelated class પર: families આરપાર shared promise, exactly BCA403 નું Printable pattern. (3) Interface IEmailable : Inherits IPrintable: interfaces extend interfaces, contracts ને stacking કરે છે. compact rule જે તમે meet કર્યું છે તે દરેક language ને survive કરે છે: inherit what you ARE, implement what you PROMISE.

Watch out

Interface fine print

No instantiation: New IPrintable() meaningless અને illegal છે; interface variables implementing objects ને HOLD કરે છે.

Implement everything: એક 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, easy marks drop કરવા.

Theory

Unit 4 OOP circle ને close કરે છે

count કરો કે 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 તેને work પર મૂકે છે જ્યાં shops live or die કરે છે: real database માં bills ને save કરવું ADO.NET સાથે, જ્યાં Unit 3 નું DataSet finally તેના DataAdapter ને meet કરે છે.

Summary

Key takeaways

  • Inherits: one parent class, is-a, state shared, behaviour માટે Overridable/Overrides.
  • Interface ... End Interface signatures ને declare કરે છે; names conventionally I થી start થાય છે.
  • class ઘણા interfaces ને Implements કરે છે; interfaces બીજા interfaces ને Inherits કરે છે.
  • દરેક implementing member ને explicit clause જોઈએ છે: Sub X() Implements IFoo.X: name-matching alone કંઈ નથી.
  • clause renamed implementations ને permit કરે છે અને 2 contracts થી same-named members ને resolve કરે છે.
  • Interface references unrelated families આરપાર polymorphism આપે છે: Dim p As IPrintable = New BillReceipt().
  • Memory hook: inherit what you are, implement what you promise, અને VB માં, say so in 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