Interfaces & Inheritance

Inherits takes one parent, Implements takes many contracts, and VB adds paperwork Java skipped: every implementing member must name its interface method with an explicit Implements clause.

11 min read · 9 cards · 2 checks

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


Theory

The receipt story, retold

End of Unit 4, and ShopKeeper faces the exact dilemma BookBridge faced in BCA403: receipts, day reports and price tags must all be printable, but they share no sensible parent, and Inherits, like extends, is a one-parent affair.

You know the answer is an interface: a contract, not a bloodline. VB.NET agrees, then adds a rule of its own: signing the contract is not enough; every method must say which clause of the contract it fulfils, in writing.

Theory

Inherits and Implements, formally

Inherits is class-to-class: Class Cashier : Inherits Person. Exactly ONE parent, members overridden via the Overridable/Overrides pair from last lesson.

Interface declares pure signatures (convention: names start with I):

Interface IPrintable

Sub PrintReceipt()

End Interface

Implements is class-to-contract, and plural is welcome:

Class BillReceipt

Implements IPrintable, IScannable

And interfaces themselves may extend interfaces, with Inherits.

Practical

Two 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

The clause Java never asked for

Look at the method line again:

Public Sub PrintReceipt() Implements IPrintable.PrintReceipt

In Java, a matching name and signature silently satisfied the interface. In VB.NET the trailing Implements clause is compulsory: without it, the method is just a method, the contract stands unfulfilled, and the class fails to compile.

The paperwork buys 2 real abilities: the method may use a different name than the interface member (the clause is the link, not the name), and when 2 interfaces demand the same-named member, separate methods can each serve their own master unambiguously.

Quiz

BillReceipt declares Implements IPrintable, and contains Public Sub PrintReceipt() with the right signature but NO trailing Implements clause. What happens?

  1. It compiles: the matching name and signature satisfy the interface, as in Java
  2. Compile error: the interface member is unimplemented; the clause is the only thing that fulfils it
  3. It compiles, but calling through an IPrintable reference throws at run time
  4. It compiles with a warning and VB wires the method automatically
Show the answer

Compile error: the interface member is unimplemented; the clause is the only thing that fulfils it

VB.NET links implementations to interface members ONLY through the explicit clause: a same-named method without it is a coincidence, not a fulfilment, so the class is reported as not implementing IPrintable.PrintReceipt. Option A is the Java reflex this question exists to catch, the sharpest single difference between the 2 languages' interfaces. Options C and D soften a hard rule: nothing is deferred to run time and nothing is auto-wired. Habit to build: type Implements IPrintable, press Enter, and let the IDE generate correctly-claused stubs.

Think first

Choose the tool, one last time

Three ShopKeeper needs: (1) Cashier IS a Person and reuses its fields, (2) receipts, reports AND price tags must all promise PrintReceipt(), (3) a future IEmailable should also count as IPrintable plus more. Assign Inherits and Implements before tapping.

Show the answer

(1) Inherits Person: true is-a with shared state; one parent is all you get and all you need. (2) Implements IPrintable on each unrelated class: a shared promise across families, exactly BCA403's Printable pattern. (3) Interface IEmailable : Inherits IPrintable: interfaces extend interfaces, stacking contracts. The compact rule that survives every language you have met: inherit what you ARE, implement what you PROMISE.

Watch out

Interface fine print

No instantiation: New IPrintable() is meaningless and illegal; interface variables HOLD implementing objects.

Implement everything: miss one member (or its clause) and the class must become MustInherit or it will not compile.

Keyword swap alert: class extends interface is Java grammar; in VB a class IMPLEMENTS an interface, an interface INHERITS an interface, a class INHERITS a class. Three sentences, three keywords, easy marks to drop.

Theory

Unit 4 closes the OOP circle

Count what ShopKeeper's classes now do: guarded Properties (encapsulation), a MustInherit Product family with GST polymorphism, staff classes navigating Me/MyBase/MyClass, access doors from Private to Protected Friend, and contracts via interfaces with honest paperwork. That is the entire OOP surface this paper examines. Unit 5 puts it to work where shops live or die: saving bills to a real database with ADO.NET, where the DataSet from Unit 3 finally meets its DataAdapter.

Summary

Key takeaways

  • Inherits: one parent class, is-a, state shared, Overridable/Overrides for behaviour.
  • Interface ... End Interface declares signatures; names conventionally start with I.
  • A class Implements many interfaces; interfaces Inherits other interfaces.
  • Every implementing member needs the explicit clause: Sub X() Implements IFoo.X: name-matching alone is nothing.
  • The clause permits renamed implementations and resolves same-named members from 2 contracts.
  • Interface references give polymorphism across unrelated families: Dim p As IPrintable = New BillReceipt().
  • Memory hook: inherit what you are, implement what you promise, and in 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

Interfaces & Inheritance · .NET Programming · Gri-Learn