Skip to content
AD-305 · Object Oriented Programming & Methodology/Quick Revision Short Notes

Object Oriented Programming & Methodology (AD-305) - Unit 3 Short Notes

How unit 3 is examined

This unit covers inheritance and its types, the is-a relationship, association, aggregation, and interfaces with abstract classes. Inheritance and interfaces/abstract classes carry most of the marks.

Inheritance: purpose and its types

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>

Definition. <mark>Inheritance is the mechanism by which a derived (child) class acquires the data members and member functions of an existing base (parent) class, so that existing code is reused and extended.</mark>

Diagram.

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-01" viewBox="0 0 307 198" width="307" height="198" role="img" aria-label="tree diagram"><style>#dsfig-u3-01 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-01 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-01 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-01 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-01 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-01 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-01 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-01 .t{fill:#16181D;font-weight:500}#dsfig-u3-01 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-01 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-01 .dot{fill:#16181D}#dsfig-u3-01 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-01 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-01 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-01 .ah{fill:#454C5A}#dsfig-u3-01 .ah.hi{fill:#2340B8}#dsfig-u3-01 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-01 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-01 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-01 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-01 .e{stroke:#B1B7C3}html.dark #dsfig-u3-01 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-01 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-01 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-01 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-01 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-01 .t{fill:#E6E8ED}html.dark #dsfig-u3-01 .t.inv{fill:#0F1115}html.dark #dsfig-u3-01 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-01 .dot{fill:#E6E8ED}html.dark #dsfig-u3-01 .ann{fill:#8FA3FF}html.dark #dsfig-u3-01 .lbl{fill:#858D9C}html.dark #dsfig-u3-01 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-01 .ah{fill:#B1B7C3}html.dark #dsfig-u3-01 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-01 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-01 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-01 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><line class="e" x1="226.5" y1="39" x2="141.5" y2="103"/><line class="e" x1="141.5" y1="103" x2="56.5" y2="167"/><rect class="n" x="193" y="24" width="67" height="30" rx="8"/><text class="t" x="226.5" y="39" dy=".35em" text-anchor="middle">Single</text><rect class="n" x="104" y="88" width="75" height="30" rx="8"/><text class="t" x="141.5" y="103" dy=".35em" text-anchor="middle">Vehicle</text><circle class="n" cx="56.5" cy="167" r="17"/><text class="t" x="56.5" y="167" dy=".35em" text-anchor="middle">Car</text></svg></figure>

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-02" viewBox="0 0 484 262" width="484" height="262" role="img" aria-label="tree diagram"><style>#dsfig-u3-02 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-02 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-02 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-02 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-02 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-02 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-02 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-02 .t{fill:#16181D;font-weight:500}#dsfig-u3-02 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-02 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-02 .dot{fill:#16181D}#dsfig-u3-02 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-02 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-02 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-02 .ah{fill:#454C5A}#dsfig-u3-02 .ah.hi{fill:#2340B8}#dsfig-u3-02 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-02 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-02 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-02 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-02 .e{stroke:#B1B7C3}html.dark #dsfig-u3-02 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-02 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-02 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-02 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-02 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-02 .t{fill:#E6E8ED}html.dark #dsfig-u3-02 .t.inv{fill:#0F1115}html.dark #dsfig-u3-02 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-02 .dot{fill:#E6E8ED}html.dark #dsfig-u3-02 .ann{fill:#8FA3FF}html.dark #dsfig-u3-02 .lbl{fill:#858D9C}html.dark #dsfig-u3-02 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-02 .ah{fill:#B1B7C3}html.dark #dsfig-u3-02 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-02 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-02 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-02 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah8" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh8" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><line class="e" x1="392" y1="39" x2="284" y2="103"/><line class="e" x1="284" y1="103" x2="176" y2="167"/><line class="e" x1="176" y1="167" x2="68" y2="231"/><rect class="n" x="343" y="24" width="98" height="30" rx="8"/><text class="t" x="392" y="39" dy=".35em" text-anchor="middle">Multilevel</text><circle class="n" cx="284" cy="103" r="17"/><text class="t" x="284" y="103" dy=".35em" text-anchor="middle">A</text><circle class="n" cx="176" cy="167" r="17"/><text class="t" x="176" y="167" dy=".35em" text-anchor="middle">B</text><circle class="n" cx="68" cy="231" r="17"/><text class="t" x="68" y="231" dy=".35em" text-anchor="middle">C</text></svg></figure>

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-03" viewBox="0 0 186 198" width="186" height="198" role="img" aria-label="tree diagram"><style>#dsfig-u3-03 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-03 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-03 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-03 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-03 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-03 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-03 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-03 .t{fill:#16181D;font-weight:500}#dsfig-u3-03 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-03 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-03 .dot{fill:#16181D}#dsfig-u3-03 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-03 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-03 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-03 .ah{fill:#454C5A}#dsfig-u3-03 .ah.hi{fill:#2340B8}#dsfig-u3-03 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-03 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-03 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-03 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-03 .e{stroke:#B1B7C3}html.dark #dsfig-u3-03 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-03 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-03 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-03 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-03 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-03 .t{fill:#E6E8ED}html.dark #dsfig-u3-03 .t.inv{fill:#0F1115}html.dark #dsfig-u3-03 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-03 .dot{fill:#E6E8ED}html.dark #dsfig-u3-03 .ann{fill:#8FA3FF}html.dark #dsfig-u3-03 .lbl{fill:#858D9C}html.dark #dsfig-u3-03 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-03 .ah{fill:#B1B7C3}html.dark #dsfig-u3-03 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-03 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-03 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-03 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah9" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh9" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><line class="e" x1="81" y1="39" x2="81" y2="103"/><line class="e" x1="81" y1="103" x2="31" y2="167"/><line class="e" x1="81" y1="103" x2="81" y2="167"/><line class="e" x1="81" y1="103" x2="131" y2="167"/><rect class="n" x="24" y="24" width="114" height="30" rx="8"/><text class="t" x="81" y="39" dy=".35em" text-anchor="middle">Hierarchical</text><circle class="n" cx="81" cy="103" r="17"/><text class="t" x="81" y="103" dy=".35em" text-anchor="middle">A</text><circle class="n" cx="31" cy="167" r="17"/><text class="t" x="31" y="167" dy=".35em" text-anchor="middle">B</text><circle class="n" cx="81" cy="167" r="17"/><text class="t" x="81" y="167" dy=".35em" text-anchor="middle">C</text><circle class="n" cx="131" cy="167" r="17"/><text class="t" x="131" y="167" dy=".35em" text-anchor="middle">D</text></svg></figure>

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-04" viewBox="0 0 338 338" width="338" height="338" role="img" aria-label="Hybrid (diamond): A is the base, B and C derive from A, D derives from both B and C. Multiple inheritance is just B and C both pointing to D."><style>#dsfig-u3-04 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-04 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-04 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-04 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-04 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-04 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-04 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-04 .t{fill:#16181D;font-weight:500}#dsfig-u3-04 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-04 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-04 .dot{fill:#16181D}#dsfig-u3-04 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-04 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-04 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-04 .ah{fill:#454C5A}#dsfig-u3-04 .ah.hi{fill:#2340B8}#dsfig-u3-04 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-04 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-04 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-04 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-04 .e{stroke:#B1B7C3}html.dark #dsfig-u3-04 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-04 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-04 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-04 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-04 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-04 .t{fill:#E6E8ED}html.dark #dsfig-u3-04 .t.inv{fill:#0F1115}html.dark #dsfig-u3-04 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-04 .dot{fill:#E6E8ED}html.dark #dsfig-u3-04 .ann{fill:#8FA3FF}html.dark #dsfig-u3-04 .lbl{fill:#858D9C}html.dark #dsfig-u3-04 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-04 .ah{fill:#B1B7C3}html.dark #dsfig-u3-04 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-04 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-04 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-04 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah10" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh10" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M155.6,53.4 L54.8,154.2" marker-end="url(#ah10)"/><path class="e" d="M182.4,53.4 L283.2,154.2" marker-end="url(#ah10)"/><path class="e" d="M53.4,182.4 L154.2,283.2" marker-end="url(#ah10)"/><path class="e" d="M284.6,182.4 L183.8,283.2" marker-end="url(#ah10)"/><circle class="n" cx="169" cy="40" r="18"/><text class="t" x="169" y="40" dy=".35em" text-anchor="middle">A</text><circle class="n" cx="40" cy="169" r="18"/><text class="t" x="40" y="169" dy=".35em" text-anchor="middle">B</text><circle class="n" cx="298" cy="169" r="18"/><text class="t" x="298" y="169" dy=".35em" text-anchor="middle">C</text><circle class="n" cx="169" cy="298" r="18"/><text class="t" x="169" y="298" dy=".35em" text-anchor="middle">D</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Hybrid (diamond): A is the base, B and C derive from A, D derives from both B and C. Multiple inheritance is just B and C both pointing to D.</figcaption></figure>

Key points.

  1. Its advantages are reusability (the derived class uses base members without rewriting them), extensibility (new features are added in a subclass without touching tested code), reduced redundancy, easier maintenance (a fix in the base reaches every subclass) and support for runtime polymorphism through base pointers and virtual functions.
  2. It models an is-a relationship, so a Car is a Vehicle and a Student is a Person.
  3. Single inheritance means one derived class has exactly one base class.
  4. Multiple inheritance means one derived class has two or more base classes, as in class C : public A, public B; a real case is a TeachingAssistant that is both a Student and an Employee.
  5. Multilevel inheritance is a chain: A to B to C, so C gets the members of both B and A.
  6. Hierarchical inheritance means several classes derive from one base class.
  7. Hybrid inheritance combines two or more of the above, for example the diamond, and it can cause the diamond problem: D receives two copies of A's members through B and C, so a use of an A member is ambiguous.
  8. Virtual inheritance, class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {};, gives D a single A; a name clash between two bases is resolved with the scope resolution operator (FATHER::Q2). Java gives multiple inheritance of type only through interfaces.
  9. Protected members are visible in derived classes but not outside, and a derived constructor calls the base one, Car(int w):Vehicle(w){}. Public inheritance keeps base access levels, protected inheritance makes public members protected, and private inheritance makes the base's public and protected members private in the derived class; base private members are never accessible in the derived class under any mode.
  10. Private members of the base are inherited (they occupy space in the derived object) but are not accessible in the derived class; protected and public ones are.

Example.

class Vehicle { public: int wheels; Vehicle(int w){ wheels=w; }
  void show(){ cout<<"Wheels: "<<wheels<<endl; } };
class Car : public Vehicle { public: Car(int w):Vehicle(w){}
  void drive(){ cout<<"Car runs"<<endl; } };
int main(){ Car c(4); c.show(); c.drive(); }
// Output: Wheels: 4
//         Car runs

Car (derived) reuses wheels and show() from Vehicle (base), and its constructor passes 4 to Vehicle(int w).

Example (multilevel, hierarchical, hybrid).

#include <iostream>
using namespace std;
class A { public: void a(){ cout<<"A "; } };
class B : public A { public: void b(){ cout<<"B "; } };   // multilevel A -> B -> C
class C : public B { public: void c(){ cout<<"C\n"; } };
class D : public A {};  class E : public A {};            // hierarchical A -> D, A -> E
class F : public B, public D {};   // hybrid: multilevel + multiple (A reached twice; use virtual A)
int main(){ C x; x.a(); x.b(); x.c(); D d; E e; d.a(); e.a(); cout<<endl; }
// Output: A B C
//         A A

Example (multiple, real world).

class Student { public: void study(){ cout<<"Attends classes\n"; } };
class Employee { public: void work(){ cout<<"Draws salary\n"; } };
class TeachingAssistant : public Student, public Employee {};
int main(){ TeachingAssistant t; t.study(); t.work(); }
// Output: Attends classes / Draws salary

Example (FATHER, MOTHER and CHILD).

#include <iostream>
#include <string>
using namespace std;
class FATHER { public: string Q, Q2, Q3;
  FATHER(){ Q="Honesty"; Q2="Kindness"; Q3="Discipline"; } };
class MOTHER { public: string Q1, Q2, Q4;
  MOTHER(){ Q1="Patience"; Q2="Kindness"; Q4="Creativity"; } };
class CHILD : public FATHER, public MOTHER { public:
  void display(){   // plain Q2 is ambiguous: both bases have a member Q2
    cout<<Q<<" "<<Q1<<" "<<FATHER::Q2<<" "<<Q3<<" "<<Q4<<endl; } };
int main(){ CHILD c; c.display(); }
// Output: Honesty Patience Kindness Discipline Creativity

CHILD inherits Q, Q2, Q3 from FATHER and Q1, Q2, Q4 from MOTHER, so it holds two Q2 members; writing FATHER::Q2 picks one, and the resolved set Q, Q1, Q2, Q3, Q4 is printed with Q2 once.

Access modifiers (short note). Public members are visible everywhere, private members only inside their own class, and protected members inside the class and its derived classes; Java adds default (package-private, no keyword) access, visible only within the same package. They matter because they enforce encapsulation and data hiding: data stays private behind public functions, which gives security against invalid outside changes and lets the class change its internals safely.

Modifier Same class Derived class Same package Outside
public Yes Yes Yes Yes
protected Yes Yes Yes (Java) No
default (Java only) Yes Only in same package Yes No
private Yes No No No
class Base { private: int a=1; protected: int b=2; public: int c=3; };
class Derived : public Base { public: void show(){ cout<<b<<" "<<c<<endl; } };  // a is an error here
int main(){ Derived d; d.show(); cout<<d.c<<endl; }   // d.a and d.b are errors here
// Output: 2 3
//         3

Answer frame. Open with the definition and the reusability purpose; draw the five small diagrams (single, multiple, multilevel, hierarchical, hybrid diamond) with class names in boxes; then develop points 1-8 with a syntax line for each type; close with "Inheritance reduces redundancy and speeds development, but multiple inheritance needs care with the diamond problem." For the multiple-inheritance role question, stress points 4, 7, 8 (advantage: combining features, as in TeachingAssistant; issue: ambiguity; fix: scope resolution or virtual inheritance in C++, interfaces in Java). For the FATHER-MOTHER program, write the two base classes with constructors, CHILD with display() using FATHER::Q2, main and the output.

Pitfall: Writing plain Q2 inside CHILD gives an "ambiguous" compile error; qualify it as FATHER::Q2 or MOTHER::Q2.

Asked: [7 marks] (Jun 2023, Dec 2024, Dec 2025) Explain inheritance and its types. Asked: [7 marks] (Jun 2023, Dec 2025) Explain the fundamental concept behind inheritance with an example. Define multiple, multilevel and hybrid inheritance with diagram and syntax. Asked: [7 marks] (Nov 2022) Write a program in C++ which demonstrates the use of inheritance. Asked: [7 marks] (Nov 2022) Design classes FATHER and MOTHER and class CHILD derived from both. FATHER has qualities Q, Q2, Q3 and MOTHER has Q1, Q2, Q4. Write a C++ program to display the qualities of CHILD. Asked: [7 marks] (Dec 2023) Describe the role of multiple inheritance in object-oriented programming.

The 'is a' relationship

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>

Definition. <mark>The is-a relationship means a derived class is a specialised kind of its base class, so a derived object can be used wherever a base object is expected.</mark>

Diagram.

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-05" viewBox="0 0 259 209" width="259" height="209" role="img" aria-label="UML convention: generalisation (is-a) is a solid line with a hollow triangle pointing from derived (Dog, Cat) to base (Animal); realisation of an interface is a dashed line with a hollow triangle. Has-a uses a diamond."><style>#dsfig-u3-05 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-05 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-05 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-05 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-05 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-05 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-05 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-05 .t{fill:#16181D;font-weight:500}#dsfig-u3-05 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-05 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-05 .dot{fill:#16181D}#dsfig-u3-05 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-05 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-05 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-05 .ah{fill:#454C5A}#dsfig-u3-05 .ah.hi{fill:#2340B8}#dsfig-u3-05 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-05 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-05 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-05 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-05 .e{stroke:#B1B7C3}html.dark #dsfig-u3-05 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-05 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-05 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-05 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-05 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-05 .t{fill:#E6E8ED}html.dark #dsfig-u3-05 .t.inv{fill:#0F1115}html.dark #dsfig-u3-05 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-05 .dot{fill:#E6E8ED}html.dark #dsfig-u3-05 .ann{fill:#8FA3FF}html.dark #dsfig-u3-05 .lbl{fill:#858D9C}html.dark #dsfig-u3-05 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-05 .ah{fill:#B1B7C3}html.dark #dsfig-u3-05 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-05 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-05 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-05 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah11" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh11" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M50.5,153.2 L110.5,63.3" marker-end="url(#ah11)"/><path class="e" d="M201.5,153.2 L141.5,63.3" marker-end="url(#ah11)"/><rect class="n" x="101" y="25" width="50" height="30" rx="15"/><text class="t" x="126" y="40" dy=".35em" text-anchor="middle">Anim</text><circle class="n" cx="40" cy="169" r="18"/><text class="t" x="40" y="169" dy=".35em" text-anchor="middle">Dog</text><circle class="n" cx="212" cy="169" r="18"/><text class="t" x="212" y="169" dy=".35em" text-anchor="middle">Cat</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">UML convention: generalisation (is-a) is a solid line with a hollow triangle pointing from derived (Dog, Cat) to base (Animal); realisation of an interface is a dashed line with a hollow triangle. Has-a uses a diamond.</figcaption></figure>

Key points.

  1. Inheritance implements is-a: Dog is an Animal, Car is a Vehicle, and the subclass adds or specialises behaviour.
  2. Substitutability (the Liskov Substitution Principle) says a derived object must be usable wherever the base is expected without breaking the program, so a function written for Animal& works with a Dog.
  3. Every derived object carries all base members; private ones are inherited but not accessible, so the derived class uses them only through public or protected functions.
  4. Has-a is different: a Car has an Engine, which is aggregation or composition, written as a member object and drawn with a diamond, not a triangle.
  5. Test before inheriting: if "B is an A" sounds wrong, use a member object instead, as in class Car { Engine e; };.
  6. A base pointer may hold a derived object, Animal *p = new Dog;, and p->speak() then runs Dog's version.
class Animal { public: virtual void speak(){ cout<<"...\n"; } };
class Dog : public Animal { public: void speak(){ cout<<"Woof\n"; } };
void talk(Animal &a){ a.speak(); }    // written for any Animal
int main(){ Dog d; talk(d); }         // Dog substituted for Animal
// Output: Woof

Asked: [7 marks] (Jun 2025) Explain the concept of the "is a" relationship in inheritance.

Association

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>

Definition. <mark>Association is a general relationship between two independent classes in which objects of one class use or are linked with objects of another, while each keeps its own lifetime.</mark>

Diagram.

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-06" viewBox="0 0 338 80" width="338" height="80" role="img" aria-label="Teacher (T) teaches Student (S): a one-way association. Use S<>T style arrows for a two-way link."><style>#dsfig-u3-06 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-06 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-06 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-06 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-06 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-06 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-06 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-06 .t{fill:#16181D;font-weight:500}#dsfig-u3-06 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-06 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-06 .dot{fill:#16181D}#dsfig-u3-06 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-06 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-06 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-06 .ah{fill:#454C5A}#dsfig-u3-06 .ah.hi{fill:#2340B8}#dsfig-u3-06 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-06 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-06 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-06 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-06 .e{stroke:#B1B7C3}html.dark #dsfig-u3-06 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-06 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-06 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-06 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-06 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-06 .t{fill:#E6E8ED}html.dark #dsfig-u3-06 .t.inv{fill:#0F1115}html.dark #dsfig-u3-06 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-06 .dot{fill:#E6E8ED}html.dark #dsfig-u3-06 .ann{fill:#8FA3FF}html.dark #dsfig-u3-06 .lbl{fill:#858D9C}html.dark #dsfig-u3-06 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-06 .ah{fill:#B1B7C3}html.dark #dsfig-u3-06 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-06 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-06 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-06 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah12" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh12" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M59,40 L277,40" marker-end="url(#ah12)"/><g class="wl"><rect x="138.3" y="31" width="61.5" height="18" rx="9"/><text class="t" x="169" y="40" dy=".35em" text-anchor="middle">teaches</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">T</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">S</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Teacher (T) teaches Student (S): a one-way association. Use S<>T style arrows for a two-way link.</figcaption></figure>

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-07" viewBox="0 0 345 80" width="345" height="80" role="img" aria-label="Aggregation in UML: Department (1) has 1..* Teachers; draw a hollow diamond at the Department end and write 1 and 1..* at the two ends."><style>#dsfig-u3-07 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-07 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-07 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-07 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-07 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-07 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-07 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-07 .t{fill:#16181D;font-weight:500}#dsfig-u3-07 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-07 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-07 .dot{fill:#16181D}#dsfig-u3-07 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-07 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-07 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-07 .ah{fill:#454C5A}#dsfig-u3-07 .ah.hi{fill:#2340B8}#dsfig-u3-07 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-07 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-07 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-07 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-07 .e{stroke:#B1B7C3}html.dark #dsfig-u3-07 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-07 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-07 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-07 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-07 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-07 .t{fill:#E6E8ED}html.dark #dsfig-u3-07 .t.inv{fill:#0F1115}html.dark #dsfig-u3-07 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-07 .dot{fill:#E6E8ED}html.dark #dsfig-u3-07 .ann{fill:#8FA3FF}html.dark #dsfig-u3-07 .lbl{fill:#858D9C}html.dark #dsfig-u3-07 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-07 .ah{fill:#B1B7C3}html.dark #dsfig-u3-07 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-07 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-07 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-07 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah13" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh13" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M66,40 L272,40"/><g class="wl"><rect x="148.6" y="31" width="40.8" height="18" rx="9"/><text class="t" x="169" y="40" dy=".35em" text-anchor="middle">1..</text></g><rect class="n" x="15" y="25" width="50" height="30" rx="15"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">Dept</text><rect class="n" x="273" y="25" width="50" height="30" rx="15"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">Tchr</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Aggregation in UML: Department (1) has 1.. Teachers; draw a hollow diamond at the Department end and write 1 and 1..* at the two ends.</figcaption></figure>

Key points.

  1. Association is a uses-a link between classes, for example a Teacher teaches Students or a Customer places Orders.
  2. The linked objects are independent: neither owns the other or controls its lifetime.
  3. It can be unidirectional (only one class knows the other) or bidirectional (both know each other).
  4. Multiplicity states how many objects take part: one-to-one (1 at both ends), one-to-many (1 and 1..) or many-to-many (*, that is 0.., at both ends), written at the line ends; the valid forms are 1, 0..1, 1.., 0.. and *.
  5. In UML it is drawn as a plain line, with an arrow for direction and the association name on the line.
  6. In code it is implemented by holding a pointer or reference to the other class's object, or by receiving it as a function argument.
  7. Aggregation (has-a, weak) and composition (part-of, strong) are special forms of association.

Example.

class Student { public: string name; };
class Teacher { public: void teach(Student &s){ cout<<"Teaching "<<s.name; } };

Teacher uses a Student but does not own it, which is association.

Comparison.

Basis Association Aggregation Composition
Meaning General link between classes Whole-part, has-a Whole-part, part-of
Ownership No owner Whole holds parts, weak Whole owns parts, strong
Lifetime Fully independent Part can outlive the whole Part dies with the whole
Strength Weakest Medium Strongest
UML Plain line Hollow diamond at the whole Filled diamond at the whole
Code Pointer or parameter Pointer to a part made outside Part object made inside
Example Teacher and Student Department and Teacher House and Room

Answer frame. Open with the definition of association; draw the two-box UML with a labelled line and the aggregation figure with multiplicities; then develop points 1-7; give the comparison table and the examples; close with "Association is the general link, and aggregation and composition are its whole-part special cases." For the combined question, add the definition of an abstract class and why it is used (see the last topic).

Asked: [7 marks] (Jun 2023, Dec 2024) Explain the concept of association and aggregation in the object-oriented approach. What is an abstract class? Why are abstract classes used? (Also asked as: short notes on association; abstract base classes.) Asked: [7 marks] (Dec 2025) Differentiate between association and aggregation.

Aggregation

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Aggregation is a has-a, whole-part relationship in which the whole holds its parts but the parts can exist independently of it.</mark>

Key points.

  1. Department has Teachers, but the Teachers still exist if the Department closes.
  2. It is a weak form of association, drawn in UML with a hollow diamond on the whole's side.
  3. Composition is the strong form: the part cannot exist without the whole, as a House and its Rooms, and it is drawn with a filled diamond.
  4. In code the aggregate keeps a pointer to a part created outside it, while the composite creates the part inside itself, so the part is destroyed with it.
class Room { public: Room(){ cout<<"Room built\n"; } ~Room(){ cout<<"Room destroyed\n"; } };
class House { Room r; };                                  // composition: part made inside
class Dept { Teacher *t; public: Dept(Teacher *p){ t=p; } };  // aggregation: part passed in
int main(){ { House h; } cout<<"After house\n"; }
// Output: Room built / Room destroyed / After house

Concept of interfaces and Abstract classes

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>

Definition. <mark>An abstract class is a class that cannot be instantiated and is meant to be extended (in C++ it must have a pure virtual function), while an interface is a contract that declares the methods a class promises to implement.</mark>

Diagram.

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-08" viewBox="0 0 259 209" width="259" height="209" role="img" aria-label="Generalisation: solid lines with hollow triangles from Circle and Rect to abstract Shape (name in italics)."><style>#dsfig-u3-08 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-08 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-08 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-08 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-08 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-08 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-08 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-08 .t{fill:#16181D;font-weight:500}#dsfig-u3-08 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-08 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-08 .dot{fill:#16181D}#dsfig-u3-08 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-08 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-08 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-08 .ah{fill:#454C5A}#dsfig-u3-08 .ah.hi{fill:#2340B8}#dsfig-u3-08 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-08 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-08 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-08 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-08 .e{stroke:#B1B7C3}html.dark #dsfig-u3-08 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-08 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-08 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-08 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-08 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-08 .t{fill:#E6E8ED}html.dark #dsfig-u3-08 .t.inv{fill:#0F1115}html.dark #dsfig-u3-08 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-08 .dot{fill:#E6E8ED}html.dark #dsfig-u3-08 .ann{fill:#8FA3FF}html.dark #dsfig-u3-08 .lbl{fill:#858D9C}html.dark #dsfig-u3-08 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-08 .ah{fill:#B1B7C3}html.dark #dsfig-u3-08 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-08 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-08 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-08 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah14" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh14" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M54.4,147.4 L114.4,57.5" marker-end="url(#ah14)"/><path class="e" d="M197.6,147.4 L137.6,57.5" marker-end="url(#ah14)"/><circle class="n" cx="126" cy="40" r="18"/><text class="t" x="126" y="40" dy=".35em" text-anchor="middle">Shp</text><rect class="n" x="15" y="154" width="50" height="30" rx="15"/><text class="t" x="40" y="169" dy=".35em" text-anchor="middle">Circ</text><rect class="n" x="187" y="154" width="50" height="30" rx="15"/><text class="t" x="212" y="169" dy=".35em" text-anchor="middle">Rect</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Generalisation: solid lines with hollow triangles from Circle and Rect to abstract Shape (name in italics).</figcaption></figure>

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-09" viewBox="0 0 259 209" width="259" height="209" role="img" aria-label="UML interface: a box marked <<interface>> Drawable; Circle and Square realise it with a dashed arrow ending in a hollow triangle."><style>#dsfig-u3-09 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-09 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-09 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-09 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-09 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-09 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-09 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-09 .t{fill:#16181D;font-weight:500}#dsfig-u3-09 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-09 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-09 .dot{fill:#16181D}#dsfig-u3-09 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-09 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-09 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-09 .ah{fill:#454C5A}#dsfig-u3-09 .ah.hi{fill:#2340B8}#dsfig-u3-09 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-09 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-09 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-09 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-09 .e{stroke:#B1B7C3}html.dark #dsfig-u3-09 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-09 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-09 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-09 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-09 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-09 .t{fill:#E6E8ED}html.dark #dsfig-u3-09 .t.inv{fill:#0F1115}html.dark #dsfig-u3-09 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-09 .dot{fill:#E6E8ED}html.dark #dsfig-u3-09 .ann{fill:#8FA3FF}html.dark #dsfig-u3-09 .lbl{fill:#858D9C}html.dark #dsfig-u3-09 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-09 .ah{fill:#B1B7C3}html.dark #dsfig-u3-09 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-09 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-09 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-09 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah15" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh15" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M54.4,147.4 L110.5,63.3" marker-end="url(#ah15)"/><path class="e" d="M201.5,153.2 L141.5,63.3" marker-end="url(#ah15)"/><rect class="n" x="101" y="25" width="50" height="30" rx="15"/><text class="t" x="126" y="40" dy=".35em" text-anchor="middle">Draw</text><rect class="n" x="15" y="154" width="50" height="30" rx="15"/><text class="t" x="40" y="169" dy=".35em" text-anchor="middle">Circ</text><circle class="n" cx="212" cy="169" r="18"/><text class="t" x="212" y="169" dy=".35em" text-anchor="middle">Sq</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">UML interface: a box marked <<interface>> Drawable; Circle and Square realise it with a dashed arrow ending in a hollow triangle.</figcaption></figure>

Key points.

  1. In C++ an abstract class needs at least one pure virtual function, virtual void process() = 0;. In Java it is marked abstract class Shape { abstract double area(); } and may even have no abstract methods.
  2. Its object cannot be created, but pointers or references to it can be, and it acts as the base class for derived classes.
  3. A derived class must override every pure virtual function, otherwise it is also abstract.
  4. An abstract class may also hold data members, constructors and ordinary methods, so it gives partial implementation.
  5. A Java interface is declared with interface and implemented with implements; before Java 8 it was 100% abstract (abstract methods and constants only), Java 8 added default and static methods, and Java 9 added private methods, so its abstract methods and constants are always public. C++ has no keyword: an interface is a class with only pure virtual functions. Syntax: interface A extends B, C { } and class X extends P implements A, B { }.
  6. A class can implement many interfaces, which gives multiple inheritance of type; but two interfaces with the same default method recreate the diamond problem, and the class must override it and pick one with X.super.m().
  7. Both define a common contract, enable runtime polymorphism and give loose coupling through the principle "program to an interface, not an implementation" (dependency inversion: high-level code depends on abstractions, not concrete classes).
  8. In design they underlie patterns and frameworks: Strategy (interchangeable algorithms behind one interface), Factory (returns an interface type), Template Method (an abstract class fixes the steps, subclasses fill them); Java's List implemented by ArrayList, JDBC's Connection, and Runnable and Comparable.

Comparison.

Basis Abstract class Interface
Methods Abstract and concrete Abstract; default and static since Java 8
Data members Any variables Only constants (public static final)
Constructor Allowed Not allowed
Access modifiers Any (private, protected, public) Public; private methods since Java 9
Multiple inheritance Java: extend only one class; C++: may derive from several abstract classes A class implements many interfaces
Keyword abstract / = 0 interface / implements
When to use Related classes sharing code and state Unrelated classes sharing a contract
Speed Traditionally slightly faster calls Slightly slower (extra lookup)
Adding a method later Add a concrete method, subclasses unaffected Breaks implementers unless it is a default method
Flexibility Less More

Example (abstract class in C++).

#include <iostream>
using namespace std;
class Shape { public: virtual double area() = 0; virtual void name() = 0; virtual ~Shape(){} };
class Circle : public Shape { double r; public: Circle(double x){ r=x; }
  double area(){ return 3.14*r*r; } void name(){ cout<<"Circle "; } };
class Rect : public Shape { double l, b; public: Rect(double x, double y){ l=x; b=y; }
  double area(){ return l*b; } void name(){ cout<<"Rect "; } };
int main(){ Shape *s[2] = { new Circle(2), new Rect(3,4) };
  for(int i=0;i<2;i++){ s[i]->name(); cout<<s[i]->area()<<endl; delete s[i]; } }
// Output: Circle 12.56
//         Rect 12
interface Shape { double area(); }                    // same idea as a Java interface
class Circle implements Shape { double r; Circle(double r){ this.r=r; }
  public double area(){ return 3.14*r*r; } }
// Shape s = new Circle(2); System.out.println(s.area());  Output: 12.56

Example (design scenario, Java).

interface PaymentGateway { void pay(double amt); }
class CardGateway implements PaymentGateway { public void pay(double a){ System.out.println("Card " + a); } }
class UpiGateway implements PaymentGateway { public void pay(double a){ System.out.println("UPI " + a); } }
class Checkout { private PaymentGateway g;      // depends only on the interface
  Checkout(PaymentGateway g){ this.g = g; }
  void order(double amt){ g.pay(amt); } }
// new Checkout(new UpiGateway()).order(499);  Output: UPI 499.0

Checkout never changes when a new gateway (say Wallet) is added, which is the open-closed benefit of designing to an interface.

Example (default-method diamond, Java).

interface X { default void m(){ System.out.println("X"); } }
interface Y { default void m(){ System.out.println("Y"); } }
class D implements X, Y { public void m(){ X.super.m(); } }   // must override, else compile error

Example (abstract Student program, C++).

#include <iostream>
using namespace std;
class Student { protected: int marks; float fee;
public: Student(int m, float f){ marks=m; fee=f; }
  virtual void process() = 0;                 // pure virtual: Student is abstract
  virtual ~Student(){} };
class Engineering : public Student { public: Engineering(int m):Student(m,90000){}
  void process(){ cout<<"Engg fee "<<fee+5000<<", "<<(marks>=40?"Pass":"Fail")<<endl; } };
class Science : public Student { public: Science(int m):Student(m,40000){}
  void process(){ cout<<"Sci fee "<<fee<<", "<<(marks>=35?"Pass":"Fail")<<endl; } };
class Medical : public Student { public: Medical(int m):Student(m,150000){}
  void process(){ cout<<"Med fee "<<fee*1.1<<", "<<(marks>=50?"Pass":"Fail")<<endl; } };
int main(){ Student *s[3] = { new Engineering(72), new Science(30), new Medical(65) };
  for(int i=0;i<3;i++){ s[i]->process(); delete s[i]; } }
// Output: Engg fee 95000, Pass / Sci fee 40000, Fail / Med fee 165000, Pass

Engineering adds a 5,000 lab fee and Medical a 10% clinical charge; the virtual destructor makes delete s[i] through a base pointer run the derived destructor too, which would otherwise be undefined behaviour.

Answer frame. Open with the two definitions; draw Shape with Circle and Rect derived and the <<interface>> realisation figure; then develop points 1-4 for abstract classes, points 5-6 for interfaces, write the first seven table rows (the compact difference table, also for the describe question) and both code snippets, and end with points 7-8; close with "Abstract classes share code and a contract, interfaces give a pure contract, and both enable loose coupling and polymorphism." For "What is an abstract class", use points 1-4, the syntax and the Student program. For the 14-mark program, write the abstract Student with marks and fee, the three derived classes, main and the output. For the role in software design, use points 7-8 and the PaymentGateway sketch.

Pitfall: Creating an object of an abstract class (Student s;) is a compile error; use a pointer to the base class.

Asked: [7 marks] (Nov 2022, Dec 2024, Dec 2025) Explain interfaces and abstract classes. Asked: [7 marks] (Jun 2023, Jun 2024) What is meant by abstract class? Asked: [7 marks] (Dec 2024, Dec 2025) Describe the concept of interfaces and abstract classes, including their role in defining common behaviour and enforcing contracts. Asked: [7 marks] (Dec 2023) What is interface? Discuss its concept. Asked: [14 marks] (Jun 2024) Write a program having student as an abstract class and create derived classes such as engineering, science, medical from it. Create their objects and process them. Asked: [7 marks] (Jun 2025) Compare interfaces and abstract classes in terms of features, use cases and flexibility. Asked: [7 marks] (Dec 2025) Discuss their role in software design.

Last-minute revision

  • Inheritance: a derived class reuses the members of a base class.
  • Five types: single, multiple, multilevel, hierarchical, hybrid.
  • Diamond problem: D gets two copies of A; fix with virtual inheritance or scope resolution.
  • Java gets multiple inheritance of type through interfaces; clashing default methods are resolved by overriding and calling X.super.m().
  • Access: public everywhere, protected in class and derived classes, private in the class only; private inheritance makes base public and protected members private.
  • Multiplicity: 1, 0..1, 1.., 0.., *; many-to-many is * at both ends.
  • Is-a means inheritance (hollow triangle); has-a means aggregation or composition (diamond).
  • Association: independent objects linked; aggregation: whole-part, parts survive; composition: parts die with the whole.
  • Abstract class: at least one pure virtual function (= 0); cannot be instantiated; give it a virtual destructor.
  • Interface: 100% abstract before Java 8; default and static methods since Java 8, private since Java 9.

Memory hooks

  • "SMMHH": Single, Multiple, Multilevel, Hierarchical, Hybrid.
  • Is-a = inherit (triangle), Has-a = contain (diamond).
  • Aggregation is a hollow diamond (parts can leave); composition is a filled diamond (parts stay).
  • Abstract class = half-built house; interface = blueprint only.

Coverage checklist

  • Inheritance: purpose and its types: types, diagram, syntax, diamond problem, access modifiers, the C++ programs (5 questions).
  • ‘is a’ relationship: definition, substitutability, UML generalisation, has-a contrast, example (1 question).
  • Association: definition, UML, three-way comparison with aggregation and composition (2 questions).
  • Aggregation: definition, composition contrast and code (no recent questions).
  • Concept of interfaces and Abstract classes: definitions, UML, comparison, Student program, role in design (7 questions).
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in