How unit 5 is examined
Strings, exceptions, threads, collections and the ATM and library case studies; exceptions and multithreading with collections carry the most marks, and short notes add new/delete, polymorphism, templates, abstract classes, type conversion and virtual friends.
Strings
<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. A string is a sequence of characters that OOP languages treat as an object of a class (String in Java, std::string in C++) instead of a raw character array.
Key points.
- A C-style string is a character array ended by the null character
'\0', whereas aStringorstd::stringobject carries its own length and methods, so it needs no null terminator or manual size handling. - Common Java operations are
length(),concat()or+,equals()for comparison,substring(),indexOf()for search,compareTo()for ordering andtoUpperCase(). - C++
std::stringoffers+for joining,length(),substr(pos, len),find(),append()and comparison with==and<, which compare contents. - Java strings are immutable, so every change creates a new object, while C++
std::stringis mutable and is changed in place. - String literals are kept in the string pool, so two equal literals share one object, while
new String("Hi")always creates a new heap object. - For repeated appends use
StringBuilder(mutable, fast, not synchronized) orStringBuffer(synchronized, so thread-safe but slower), and compare contents withequals(), never==, which compares references. - A C
char*string allocated withmallocornew[]leaks memory if it is not freed, whilestd::stringfrees its own buffer in its destructor (RAII).
Example.
String a = "Hi", b = "Hi", c = new String("Hi");
a == b; a == c; a.equals(c); // true false true
"apple".compareTo("banana"); // -1 ('a' - 'b')
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 3; i++) sb.append(i); // "123", one object
string t = "Hi"; t += "!"; t[0] = 'h'; // mutable: "hi!"
cout << t.substr(0,2) << t.find("!"); // hi2
Asked: [7 marks] (Dec 2024) Explain how strings are handled in object-oriented programming, including common operations and best practices for string manipulation.
Exception handling
<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 exception is a runtime event that disrupts the normal flow of a program, and exception handling is the mechanism that catches it and lets the program recover or end gracefully.</mark>
Diagram. <figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-01" viewBox="0 0 755 258" width="755" height="258" role="img" aria-label="Java exception hierarchy. Throwable is the root; Error is normally not caught; IOException is checked; RuntimeException subclasses are unchecked"><style>#dsfig-u5-01 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-01 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-01 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-01 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-01 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-01 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-01 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-01 .t{fill:#16181D;font-weight:500}#dsfig-u5-01 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-01 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-01 .dot{fill:#16181D}#dsfig-u5-01 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-01 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-01 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-01 .ah{fill:#454C5A}#dsfig-u5-01 .ah.hi{fill:#2340B8}#dsfig-u5-01 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-01 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-01 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-01 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-01 .e{stroke:#B1B7C3}html.dark #dsfig-u5-01 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-01 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-01 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-01 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-01 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-01 .t{fill:#E6E8ED}html.dark #dsfig-u5-01 .t.inv{fill:#0F1115}html.dark #dsfig-u5-01 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-01 .dot{fill:#E6E8ED}html.dark #dsfig-u5-01 .ann{fill:#8FA3FF}html.dark #dsfig-u5-01 .lbl{fill:#858D9C}html.dark #dsfig-u5-01 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-01 .ah{fill:#B1B7C3}html.dark #dsfig-u5-01 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-01 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-01 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-01 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah20" 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="ahh20" 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="232.4" y1="37" x2="86.5" y2="101"/><line class="e" x1="232.4" y1="37" x2="378.3" y2="101"/><line class="e" x1="86.5" y1="101" x2="86.5" y2="165"/><line class="e" x1="378.3" y1="101" x2="228" y2="165"/><line class="e" x1="378.3" y1="101" x2="528.5" y2="165"/><line class="e" x1="528.5" y1="165" x2="389" y2="229"/><line class="e" x1="528.5" y1="165" x2="550" y2="229"/><line class="e" x1="528.5" y1="165" x2="668" y2="229"/><rect class="n" x="186.9" y="22" width="91" height="30" rx="8"/><text class="t" x="232.4" y="37" dy=".35em" text-anchor="middle">Throwable</text><rect class="n" x="57" y="86" width="59" height="30" rx="8"/><text class="t" x="86.5" y="101" dy=".35em" text-anchor="middle">Error</text><rect class="n" x="14" y="150" width="145" height="30" rx="8"/><text class="t" x="86.5" y="165" dy=".35em" text-anchor="middle">OutOfMemoryError</text><rect class="n" x="332.8" y="86" width="91" height="30" rx="8"/><text class="t" x="378.3" y="101" dy=".35em" text-anchor="middle">Exception</text><rect class="n" x="175" y="150" width="106" height="30" rx="8"/><text class="t" x="228" y="165" dy=".35em" text-anchor="middle">IOException</text><rect class="n" x="456" y="150" width="145" height="30" rx="8"/><text class="t" x="528.5" y="165" dy=".35em" text-anchor="middle">RuntimeException</text><rect class="n" x="297" y="214" width="184" height="30" rx="8"/><text class="t" x="389" y="229" dy=".35em" text-anchor="middle">ArrayIndexOutOfBounds</text><rect class="n" x="497" y="214" width="106" height="30" rx="8"/><text class="t" x="550" y="229" dy=".35em" text-anchor="middle">NullPointer</text><rect class="n" x="619" y="214" width="98" height="30" rx="8"/><text class="t" x="668" y="229" dy=".35em" text-anchor="middle">Arithmetic</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Java exception hierarchy. Throwable is the root; Error is normally not caught; IOException is checked; RuntimeException subclasses are unchecked</figcaption></figure>
Key points.
- Compile-time errors, such as a missing semicolon, are found by the compiler before the program runs, while runtime errors, such as division by zero, occur during execution and are the ones exception handling deals with.
- An Error, such as running out of memory, can technically be caught but normally should not be, because the program cannot recover from it, while an Exception is meant to be handled.
- Checked exceptions (IOException) must be caught or declared, but unchecked ones (RuntimeException and subclasses) need not be.
- The
tryblock holds the risky code, control jumps to the matchingcatchblock as soon as an exception occurs, and several catch blocks may follow one try. - The
finallyblock (Java only) always runs, whether or not an exception occurred, so it closes files and releases resources. throw(Java and C++) raises an exception, andthrows(Java only) in the method header declares the exceptions passed to the caller;try,catchandthroware common to both languages.- Over error codes, exceptions separate error logic from normal logic, cannot be silently ignored, propagate up the call stack automatically, and group related errors by type.
- Propagation (stack unwinding): the rest of the try is skipped, local objects are destroyed (C++ destructors run), each caller is searched for a matching catch, and an uncaught exception ends the program (
terminate()in C++, a stack trace in Java). - Best practices are to catch specific exceptions, use try-with-resources,
try (FileReader f = new FileReader("a.txt")) {...}, never use exceptions for normal flow, avoid empty catch blocks because they hide the fault, and log or rethrow (throw e;in Java,throw;in C++) instead; custom exceptions useclass LowBalance extends Exception.
Example.
try {
int[] a = new int[3];
a[5] = 10; // illegal index
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Bad index"); // Bad index
} finally {
System.out.println("Done"); // Done
}
Throw vs catch.
| Basis | throw | catch |
|---|---|---|
| Role | Signals that an error has occurred | Intercepts and handles the error |
| Where | Inside a method or try block | In the handler after try |
| Form | throw new Exception("msg"); |
catch (Exception e) { ... } |
| Effect | Stops normal flow | Restores normal flow |
Specific exceptions. ArrayIndexOutOfBoundsException occurs when an index is below 0 or at least the array length, as in a[5] for size 3. NullPointerException occurs when a method or field is used on a null reference, as in String s = null; s.length();. Prevent them by checking i < a.length and s != null.
C++ exceptions and templates (Nov 2022). C++ uses try, throw and catch; any value or object can be thrown, and catch(...) catches every type. A function template is one generic function for many types, and a class template is one generic class. template <typename T> means the same as <class T>, a template may take several parameters (template <class T, class U>), the type can be given explicitly as in big<double>(3, 2.5) (gives 3), and the compiler generates a separate instance for each type used.
template <class T> T big(T a, T b) { return a > b ? a : b; }
template <class T> class Stack {
T s[10]; int top = -1;
public:
void push(T x) { s[++top] = x; }
T pop() { return s[top--]; }
};
cout << big(3, 7) << big(2.5, 1.5); // 7 2.5
Stack<int> st; st.push(5); cout << st.pop(); // 5
try { throw 10; }
catch (int e) { cout << "Caught " << e; } // Caught 10
catch (...) { cout << "Other"; } // any other type
New and delete (C++). new allocates memory on the heap at runtime and returns its address, and delete frees it. new also calls the constructor and delete calls the destructor. Forgetting delete causes a memory leak, and using a pointer after delete is a dangling pointer, so set it to nullptr. Mixing forms, new[] with delete or new with free, is undefined behaviour; new (nothrow) int[n] returns nullptr instead of throwing bad_alloc.
int n = 4;
int *p = new int(5); // single object, value 5
int *a = new int[n]; // array of n ints
delete p; delete[] a; // free both
p = nullptr; // avoid a dangling pointer
| Basis | new / delete | malloc / free |
|---|---|---|
| Nature | Operators | Library functions |
| Size | Computed from the type | Given in bytes, sizeof |
| Return | Typed pointer, no cast | void*, needs a cast |
| Constructor | Calls constructor and destructor | Calls neither |
| Failure | Throws bad_alloc |
Returns NULL |
| Overloading | Can be overloaded | Cannot |
Polymorphism. Polymorphism means "many forms": one name (function, operator or method call) behaves differently according to its arguments or its object. It gives reusability and extensibility, because a new subclass works with old code unchanged.
| Basis | Compile-time (static) | Run-time (dynamic) |
|---|---|---|
| Achieved by | Function and operator overloading | Virtual functions, method overriding |
| Binding | Early, by the compiler | Late, through the vtable |
| Decided by | Argument list | Actual object type |
| Speed | Faster | Slightly slower |
| Inheritance | Not needed | Needed |
int add(int a, int b) { return a + b; } // overloading
double add(double a, double b) { return a + b; }
class Shape { public: virtual void draw() { cout << "Shape"; } };
class Circle : public Shape { public: void draw() { cout << "Circle"; } };
Shape *s = new Circle; s->draw(); // Circle (dynamic binding)
Answer frame. Open with the definition; draw the hierarchy; develop points 1-9, then the code; close with graceful recovery. Throw vs catch: table plus code. Named exceptions: definition, cause, code, prevention each. Short notes: new/delete with syntax, leak, dangling pointer and table; polymorphism with table, both codes and benefits.
Pitfall: Catching a superclass such as Exception before a subclass makes the subclass catch unreachable, and it is a compile error.
Asked: [7 marks] (Dec 2023, Jun 2023, Dec 2024, Jun 2025) Explain the concept of exception handling in JAVA. What is meant by exceptions? How is an exception handled? Best practices? Asked: [14 marks] (Nov 2022, Jun 2024) Write short notes on (any two): i) Exception handling ii) New and delete iii) Polymorphism Asked: [7 marks] (Jun 2023, Jun 2024) What do you mean by Array Index Out of Bounds Exception and Null Pointer Exception? Asked: [7 marks] (Jun 2025) What is the difference between throwing and catching an exception? Provide a code example. Asked: [7 marks] (Nov 2022) Explain in detail about exception and how they are handled? Define function template and class template. Asked: [7 marks] (Nov 2022, Dec 2025) Explain exception handling and multithreading.
Multi-threading and data collections
<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>A thread is a lightweight sub-process, and multithreading is the concurrent execution of two or more threads within one program to use the CPU fully.</mark>
Diagram. <figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-02" viewBox="0 0 467 209" width="467" height="209" role="img" aria-label="Thread states. New; Run = Runnable; Rng = Running; Blk = Blocked or Waiting; End = Terminated"><style>#dsfig-u5-02 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-02 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-02 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-02 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-02 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-02 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-02 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-02 .t{fill:#16181D;font-weight:500}#dsfig-u5-02 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-02 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-02 .dot{fill:#16181D}#dsfig-u5-02 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-02 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-02 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-02 .ah{fill:#454C5A}#dsfig-u5-02 .ah.hi{fill:#2340B8}#dsfig-u5-02 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-02 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-02 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-02 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-02 .e{stroke:#B1B7C3}html.dark #dsfig-u5-02 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-02 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-02 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-02 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-02 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-02 .t{fill:#E6E8ED}html.dark #dsfig-u5-02 .t.inv{fill:#0F1115}html.dark #dsfig-u5-02 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-02 .dot{fill:#E6E8ED}html.dark #dsfig-u5-02 .ann{fill:#8FA3FF}html.dark #dsfig-u5-02 .lbl{fill:#858D9C}html.dark #dsfig-u5-02 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-02 .ah{fill:#B1B7C3}html.dark #dsfig-u5-02 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-02 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-02 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-02 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah21" 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="ahh21" 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 L148,40" marker-end="url(#ah21)"/><path class="e" d="M188,40 L277,40" marker-end="url(#ah21)"/><path class="e" d="M284.6,53.4 L183.8,154.2" marker-end="url(#ah21)"/><path class="e" d="M169,150 L169,61" marker-end="url(#ah21)"/><path class="e" d="M317,40 L406,40" marker-end="url(#ah21)"/><g class="wl"><rect x="81" y="31" width="47.1" height="18" rx="9"/><text class="t" x="104.5" y="40" dy=".35em" text-anchor="middle">start</text></g><g class="wl"><rect x="195.6" y="31" width="75.9" height="18" rx="9"/><text class="t" x="233.5" y="40" dy=".35em" text-anchor="middle">scheduler</text></g><g class="wl"><rect x="213.1" y="95.5" width="40.8" height="18" rx="9"/><text class="t" x="233.5" y="104.5" dy=".35em" text-anchor="middle">wait</text></g><g class="wl"><rect x="141.9" y="95.5" width="54.3" height="18" rx="9"/><text class="t" x="169" y="104.5" dy=".35em" text-anchor="middle">notify</text></g><g class="wl"><rect x="342.1" y="31" width="40.8" height="18" rx="9"/><text class="t" x="362.5" y="40" dy=".35em" text-anchor="middle">done</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">New</text><circle class="n" cx="169" cy="40" r="18"/><text class="t" x="169" y="40" dy=".35em" text-anchor="middle">Run</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">Rng</text><circle class="n" cx="169" cy="169" r="18"/><text class="t" x="169" y="169" dy=".35em" text-anchor="middle">Blk</text><circle class="n" cx="427" cy="40" r="18"/><text class="t" x="427" y="40" dy=".35em" text-anchor="middle">End</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Thread states. New; Run = Runnable; Rng = Running; Blk = Blocked or Waiting; End = Terminated</figcaption></figure>
Key points.
- A thread is created by extending the
Threadclass or by implementing theRunnableinterface, and both overriderun(). - Calling
start()moves a thread from New to Runnable, and the scheduler then runs it, so callingrun()directly does not create a new thread. - A thread moves to Blocked or Waiting when it waits for a lock,
sleep()orwait(), and to Terminated whenrun()finishes. - Benefits are maximum CPU use, a responsive user interface, parallel execution and less idle time.
- Threads that share an object can interleave a read-modify-write such as
count++, losing updates (a race condition), sosynchronizedmethods let only one thread enter at a time. wait()releases the lock and pauses a thread until another thread callsnotify()(ornotifyAll()) on the same object, which is how producer and consumer threads coordinate.- A collection is an object that groups other objects: List (
ArrayList,LinkedList) keeps order and duplicates, Set (HashSetunordered,TreeSetsorted) keeps unique items, Queue (LinkedList,PriorityQueue;offer,poll) serves FIFO, and Map (HashMap;put,get) looks up by key, but Map is not a subtype ofCollection. - Collections give dynamic size, type safety through generics (
List<Book>), and ready algorithms such asCollections.sort()andbinarySearch(); the C++ STL equivalents arevector,list,set,queueandmap. - For concurrent systems,
ConcurrentHashMapandCollections.synchronizedList(list)are thread-safe, so many ATM threads can update them without corrupting data.
Example.
class Counter { int count = 0;
synchronized void inc() { count++; } } // without synchronized: lost updates
class Worker extends Thread {
Counter c; Worker(Counter c) { this.c = c; }
public void run() { for (int i = 0; i < 1000; i++) c.inc(); }
}
Counter c = new Counter();
Worker t1 = new Worker(c), t2 = new Worker(c);
t1.start(); t2.start(); t1.join(); t2.join();
System.out.println(c.count); // 2000; without synchronized often less, e.g. 1873
Runnable. Implementing Runnable leaves the class free to extend another class.
class Task implements Runnable { public void run() { System.out.println("Hi"); } }
new Thread(new Task()).start(); // Hi
Collections sketch (ATM/Library).
Map<String, Account> accounts = new ConcurrentHashMap<>(); // thread-safe
accounts.put("A101", new Account(5000));
Account acc = accounts.get("A101"); // lookup by account number
List<Transaction> log = new ArrayList<>();
log.add(new Withdrawal(acc, 1000));
for (Transaction t : log) t.execute(); // balance 4000
Set<String> ids = new TreeSet<>(List.of("B2", "A1", "A1")); // [A1, B2]
Queue<String> q = new LinkedList<>(); q.offer("req1"); q.poll(); // req1
Message passing. Message passing is how objects communicate: the sender calls a method of the receiver, and the receiver's data is changed only by its own methods, which preserves encapsulation. A message has three components: the receiver object, the method name and the arguments, as in acc.withdraw(500). A synchronous message makes the sender wait for the reply, while an asynchronous message (for example, one sent to another thread) lets it continue. With dynamic binding the receiver's actual class decides at run time which method runs. The receiver may return a value, such as the new balance.
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-03" viewBox="0 0 338 80" width="338" height="80" role="img" aria-label="Message passing. Snd = sender object; Rcv = receiver object"><style>#dsfig-u5-03 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-03 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-03 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-03 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-03 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-03 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-03 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-03 .t{fill:#16181D;font-weight:500}#dsfig-u5-03 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-03 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-03 .dot{fill:#16181D}#dsfig-u5-03 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-03 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-03 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-03 .ah{fill:#454C5A}#dsfig-u5-03 .ah.hi{fill:#2340B8}#dsfig-u5-03 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-03 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-03 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-03 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-03 .e{stroke:#B1B7C3}html.dark #dsfig-u5-03 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-03 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-03 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-03 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-03 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-03 .t{fill:#E6E8ED}html.dark #dsfig-u5-03 .t.inv{fill:#0F1115}html.dark #dsfig-u5-03 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-03 .dot{fill:#E6E8ED}html.dark #dsfig-u5-03 .ann{fill:#8FA3FF}html.dark #dsfig-u5-03 .lbl{fill:#858D9C}html.dark #dsfig-u5-03 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-03 .ah{fill:#B1B7C3}html.dark #dsfig-u5-03 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-03 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-03 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-03 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah22" 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="ahh22" 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="M58.4,44.6 Q169,72 277.6,45.1" marker-end="url(#ah22)"/><path class="e" d="M279.6,35.4 Q169,8 60.4,34.9" marker-end="url(#ah22)"/><g class="wl"><rect x="130.6" y="49.4" width="75.9" height="18" rx="9"/><text class="t" x="168.5" y="58.4" dy=".35em" text-anchor="middle">msg(args)</text></g><g class="wl"><rect x="142.3" y="12.6" width="54.3" height="18" rx="9"/><text class="t" x="169.5" y="21.6" dy=".35em" text-anchor="middle">return</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">Snd</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">Rcv</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Message passing. Snd = sender object; Rcv = receiver object</figcaption></figure>
double bal = acc.withdraw(500); // receiver acc, method withdraw, argument 500; returns the new balance
Static polymorphism. It is compile-time binding through function or operator overloading; see the table above.
int sq(int x) { return x*x; } double sq(double x) { return x*x; }
sq(3); sq(2.5); // compiler picks the version by argument type: 9 6.25
Complex operator+(Complex b); // operator overloading: c1 + c2
Abstract classes. An abstract class has at least one pure virtual function, virtual void area() = 0;, cannot be instantiated, and forces every derived class to override that function, or the derived class stays abstract too. It may still hold constructors, data members and concrete functions, and pointers or references to it are allowed. Java uses the abstract keyword.
class Shape { public: virtual double area() = 0; }; // abstract
class Circle : public Shape { double r;
public: Circle(double x) : r(x) {}
double area() { return 3.14 * r * r; } };
Shape *s = new Circle(2); cout << s->area(); // 12.56
// Shape x; -> error: cannot instantiate an abstract class
Type conversion. Implicit conversion (coercion or promotion) is automatic, as int to double in double d = 5;, but narrowing such as double to int loses data; explicit casting is written by the programmer (int i = (int)3.7; gives 3). For classes: basic to class uses a one-argument constructor, class to basic a conversion operator operator int(), and class to class a constructor in the destination class or a conversion operator in the source. Marking a constructor explicit blocks its silent use. The four C++ casts are static_cast (checked, everyday), dynamic_cast (safe downcast, checked at run time), const_cast (adds or removes const) and reinterpret_cast (raw bit reinterpretation).
class Meter { int m;
public: Meter(int x) : m(x) {} // basic to class
operator int() { return m; } }; // class to basic
class Cm { int c; public: Cm(Meter x) : c(int(x) * 100) {} }; // class to class
Meter d = 5; int n = d; Cm k = d; // n = 5, k.c = 500
Virtual friend function. A friend function cannot be virtual, because it is not a member of the class and virtual dispatch works only through an object's member functions. The workaround is a friend that calls a virtual member function, so the call is still bound at run time.
class Base { public: virtual void print(ostream &o) { o << "Base"; }
friend ostream& operator<<(ostream &o, Base &b) { b.print(o); return o; } };
class Der : public Base { public: void print(ostream &o) { o << "Der"; } };
Der d; Base &b = d; cout << b; // Der
Answer frame. Open by defining multithreading; draw the state diagram; develop points 1-6, benefits, then the code. Collections: define, points 7-9, the sketch, then case-study use. Message passing: define, three components, sync vs async, code. The 14-mark design question joins concurrency, collections and the case study.
Asked: [7 marks] (Dec 2023) Explain the concept of multithreading in object-oriented programming approach. Write its benefits. Asked: [14 marks] (Dec 2023) Write short notes on (any two): a) Data Collection b) Message Passing c) Static polymorphism Asked: [14 marks] (Dec 2024) Introduce the concepts of multi-threading and data collections in OOP and discuss their applications in concurrent and data-intensive systems, with a case study such as ATM or library management system. Asked: [14 marks] (Jun 2025) Write short notes (any three): i) Multi-threading ii) Virtual Friend function iii) Abstract classes iv) Data collection v) Type conversion Asked: [7 marks] (Dec 2025) Discuss data collections with a suitable case study (ATM/Library).
Case study: ATM and library management
<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">Medium weight</span>
Definition. A case study applies object-oriented thinking by finding the classes, attributes, methods and relationships of a real system, then drawing them as a UML class diagram.
Diagram. <figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-04" viewBox="0 0 345 252" width="345" height="252" role="img" aria-label="ATM classes with multiplicity. Cus = Customer; Acc = Account (Savings or Current); Txn = Transaction (Withdrawal, Deposit, Transfer); Bnk = Bank"><style>#dsfig-u5-04 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-04 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-04 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-04 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-04 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-04 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-04 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-04 .t{fill:#16181D;font-weight:500}#dsfig-u5-04 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-04 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-04 .dot{fill:#16181D}#dsfig-u5-04 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-04 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-04 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-04 .ah{fill:#454C5A}#dsfig-u5-04 .ah.hi{fill:#2340B8}#dsfig-u5-04 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-04 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-04 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-04 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-04 .e{stroke:#B1B7C3}html.dark #dsfig-u5-04 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-04 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-04 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-04 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-04 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-04 .t{fill:#E6E8ED}html.dark #dsfig-u5-04 .t.inv{fill:#0F1115}html.dark #dsfig-u5-04 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-04 .dot{fill:#E6E8ED}html.dark #dsfig-u5-04 .ann{fill:#8FA3FF}html.dark #dsfig-u5-04 .lbl{fill:#858D9C}html.dark #dsfig-u5-04 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-04 .ah{fill:#B1B7C3}html.dark #dsfig-u5-04 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-04 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-04 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-04 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah23" 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="ahh23" 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 L143,40"/><path class="e" d="M195,40 L279,40"/><path class="e" d="M150,212 L59,212"/><path class="e" d="M180.4,196.8 L286.6,55.2"/><path class="e" d="M298,59 L298,193"/><path class="e" d="M40,59 L40,193"/><g class="wl"><rect x="73.8" y="31" width="61.5" height="18" rx="9"/><text class="t" x="104.5" y="40" dy=".35em" text-anchor="middle">1..1..</text></g><g class="wl"><rect x="213.1" y="31" width="40.8" height="18" rx="9"/><text class="t" x="233.5" y="40" dy=".35em" text-anchor="middle">1..1</text></g><g class="wl"><rect x="84.1" y="203" width="40.8" height="18" rx="9"/><text class="t" x="104.5" y="212" dy=".35em" text-anchor="middle">1..</text></g><g class="wl"><rect x="213.1" y="117" width="40.8" height="18" rx="9"/><text class="t" x="233.5" y="126" dy=".35em" text-anchor="middle">1..</text></g><g class="wl"><rect x="267.3" y="117" width="61.5" height="18" rx="9"/><text class="t" x="298" y="126" dy=".35em" text-anchor="middle">1..0..</text></g><g class="wl"><rect x="19.6" y="117" width="40.8" height="18" rx="9"/><text class="t" x="40" y="126" dy=".35em" text-anchor="middle">uses</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">Cus</text><rect class="n" x="144" y="25" width="50" height="30" rx="15"/><text class="t" x="169" y="40" dy=".35em" text-anchor="middle">Card</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">Acc</text><circle class="n" cx="40" cy="212" r="18"/><text class="t" x="40" y="212" dy=".35em" text-anchor="middle">ATM</text><circle class="n" cx="169" cy="212" r="18"/><text class="t" x="169" y="212" dy=".35em" text-anchor="middle">Bnk</text><circle class="n" cx="298" cy="212" r="18"/><text class="t" x="298" y="212" dy=".35em" text-anchor="middle">Txn</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">ATM classes with multiplicity. Cus = Customer; Acc = Account (Savings or Current); Txn = Transaction (Withdrawal, Deposit, Transfer); Bnk = Bank</figcaption></figure>
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-05" viewBox="0 0 338 424" width="338" height="424" role="img" aria-label="ATM use-case diagram. Actor Customer; use cases Authenticate, Withdraw, Deposit, Check Balance, Transfer"><style>#dsfig-u5-05 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-05 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-05 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-05 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-05 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-05 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-05 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-05 .t{fill:#16181D;font-weight:500}#dsfig-u5-05 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-05 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-05 .dot{fill:#16181D}#dsfig-u5-05 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-05 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-05 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-05 .ah{fill:#454C5A}#dsfig-u5-05 .ah.hi{fill:#2340B8}#dsfig-u5-05 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-05 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-05 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-05 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-05 .e{stroke:#B1B7C3}html.dark #dsfig-u5-05 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-05 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-05 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-05 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-05 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-05 .t{fill:#E6E8ED}html.dark #dsfig-u5-05 .t.inv{fill:#0F1115}html.dark #dsfig-u5-05 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-05 .dot{fill:#E6E8ED}html.dark #dsfig-u5-05 .ann{fill:#8FA3FF}html.dark #dsfig-u5-05 .lbl{fill:#858D9C}html.dark #dsfig-u5-05 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-05 .ah{fill:#B1B7C3}html.dark #dsfig-u5-05 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-05 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-05 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-05 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah24" 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="ahh24" 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="M55.8,201.5 L282.2,50.5"/><path class="e" d="M58,206 L280,132"/><path class="e" d="M59,212 L279,212"/><path class="e" d="M58,218 L280,292"/><path class="e" d="M55.8,222.5 L282.2,373.5"/><circle class="n" cx="40" cy="212" r="18"/><text class="t" x="40" y="212" dy=".35em" text-anchor="middle">Cus</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">Au</text><circle class="n" cx="298" cy="126" r="18"/><text class="t" x="298" y="126" dy=".35em" text-anchor="middle">Wd</text><circle class="n" cx="298" cy="212" r="18"/><text class="t" x="298" y="212" dy=".35em" text-anchor="middle">Dp</text><circle class="n" cx="298" cy="298" r="18"/><text class="t" x="298" y="298" dy=".35em" text-anchor="middle">CB</text><circle class="n" cx="298" cy="384" r="18"/><text class="t" x="298" y="384" dy=".35em" text-anchor="middle">Tr</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">ATM use-case diagram. Actor Customer; use cases Authenticate, Withdraw, Deposit, Check Balance, Transfer</figcaption></figure>
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-06" viewBox="0 0 431 252" width="431" height="252" role="img" aria-label="Library classes with multiplicity. Lib = Library; Mem = Member (Student, Faculty); Card = LibraryCard"><style>#dsfig-u5-06 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-06 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-06 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-06 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-06 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-06 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-06 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-06 .t{fill:#16181D;font-weight:500}#dsfig-u5-06 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-06 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-06 .dot{fill:#16181D}#dsfig-u5-06 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-06 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-06 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-06 .ah{fill:#454C5A}#dsfig-u5-06 .ah.hi{fill:#2340B8}#dsfig-u5-06 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-06 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-06 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-06 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-06 .e{stroke:#B1B7C3}html.dark #dsfig-u5-06 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-06 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-06 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-06 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-06 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-06 .t{fill:#E6E8ED}html.dark #dsfig-u5-06 .t.inv{fill:#0F1115}html.dark #dsfig-u5-06 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-06 .dot{fill:#E6E8ED}html.dark #dsfig-u5-06 .ann{fill:#8FA3FF}html.dark #dsfig-u5-06 .lbl{fill:#858D9C}html.dark #dsfig-u5-06 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-06 .ah{fill:#B1B7C3}html.dark #dsfig-u5-06 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-06 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-06 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-06 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah25" 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="ahh25" 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 L186,40"/><path class="e" d="M358,40 L238,40"/><path class="e" d="M384,193 L384,66"/><path class="e" d="M365,212 L238,212"/><g class="wl"><rect x="105.6" y="31" width="40.8" height="18" rx="9"/><text class="t" x="126" y="40" dy=".35em" text-anchor="middle">1..</text></g><g class="wl"><rect x="277.6" y="31" width="40.8" height="18" rx="9"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">..1</text></g><g class="wl"><rect x="353.3" y="117" width="61.5" height="18" rx="9"/><text class="t" x="384" y="126" dy=".35em" text-anchor="middle">1..0..*</text></g><g class="wl"><rect x="277.6" y="203" width="40.8" height="18" rx="9"/><text class="t" x="298" y="212" dy=".35em" text-anchor="middle">1..1</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">Lib</text><rect class="n" x="187" y="25" width="50" height="30" rx="15"/><text class="t" x="212" y="40" dy=".35em" text-anchor="middle">Book</text><rect class="n" x="359" y="25" width="50" height="30" rx="15"/><text class="t" x="384" y="40" dy=".35em" text-anchor="middle">Loan</text><circle class="n" cx="384" cy="212" r="18"/><text class="t" x="384" y="212" dy=".35em" text-anchor="middle">Mem</text><rect class="n" x="187" y="197" width="50" height="30" rx="15"/><text class="t" x="212" y="212" dy=".35em" text-anchor="middle">Card</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Library classes with multiplicity. Lib = Library; Mem = Member (Student, Faculty); Card = LibraryCard</figcaption></figure>
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-07" viewBox="0 0 517 338" width="517" height="338" role="img" aria-label="Library use-case diagram. Actors Member and Librarian; use cases Search Book, Issue Book, Return Book, Pay Fine"><style>#dsfig-u5-07 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-07 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-07 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-07 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-07 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-07 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-07 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-07 .t{fill:#16181D;font-weight:500}#dsfig-u5-07 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-07 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-07 .dot{fill:#16181D}#dsfig-u5-07 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-07 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-07 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-07 .ah{fill:#454C5A}#dsfig-u5-07 .ah.hi{fill:#2340B8}#dsfig-u5-07 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-07 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-07 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-07 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-07 .e{stroke:#B1B7C3}html.dark #dsfig-u5-07 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-07 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-07 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-07 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-07 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-07 .t{fill:#E6E8ED}html.dark #dsfig-u5-07 .t.inv{fill:#0F1115}html.dark #dsfig-u5-07 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-07 .dot{fill:#E6E8ED}html.dark #dsfig-u5-07 .ann{fill:#8FA3FF}html.dark #dsfig-u5-07 .lbl{fill:#858D9C}html.dark #dsfig-u5-07 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-07 .ah{fill:#B1B7C3}html.dark #dsfig-u5-07 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-07 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-07 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-07 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah26" 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="ahh26" 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="M57.6,118.9 L230.9,49.7"/><path class="e" d="M59,126 L236,126"/><path class="e" d="M57.6,133.1 L237.4,204.9"/><path class="e" d="M451.4,165.3 L273.6,129.7"/><path class="e" d="M451.4,172.7 L273.6,208.3"/><path class="e" d="M453.7,178.8 L277.3,284.6"/><circle class="n" cx="40" cy="126" r="18"/><text class="t" x="40" y="126" dy=".35em" text-anchor="middle">Mem</text><rect class="n" x="230" y="25" width="50" height="30" rx="15"/><text class="t" x="255" y="40" dy=".35em" text-anchor="middle">Srch</text><circle class="n" cx="255" cy="126" r="18"/><text class="t" x="255" y="126" dy=".35em" text-anchor="middle">Iss</text><circle class="n" cx="255" cy="212" r="18"/><text class="t" x="255" y="212" dy=".35em" text-anchor="middle">Ret</text><rect class="n" x="230" y="283" width="50" height="30" rx="15"/><text class="t" x="255" y="298" dy=".35em" text-anchor="middle">Fine</text><circle class="n" cx="470" cy="169" r="18"/><text class="t" x="470" y="169" dy=".35em" text-anchor="middle">Lbr</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Library use-case diagram. Actors Member and Librarian; use cases Search Book, Issue Book, Return Book, Pay Fine</figcaption></figure>
Key points.
- ATM classes are Bank, ATM, Customer, Card, Account and Transaction, and each holds its own data and methods (encapsulation).
- Customer has
nameandpin, Card hascardNoandexpiry, Bank hasbankNameandifsc, ATM hasatmId,locationandauthenticate(),withdraw(),deposit(),checkBalance(),transfer(), Account hasaccountNo,balance,debit()andcredit(), and Transaction hastxnId,amountanddate. - Savings and Current inherit from Account, and an abstract Transaction has
execute(), which Withdrawal, Deposit and Transfer override, soexecute()is polymorphic. - Multiplicities are Bank 1 to * ATM, Bank 1 to * Account, Customer 1 to 1..* Card, and Card 1 to 1 Account.
- For the library, the classes are Library, Book, Member, LibraryCard, Librarian and Loan, with methods
issueBook(),returnBook(),calculateFine()andsearchBook(). - Member has
memberId,nameandbooksIssuedwithborrow()andreturnBook()(notreturn(), a reserved word), LibraryCard hascardNoandvalidTill, and Student and Faculty extend Member with different borrowing limits (inheritance). - Librarian has
empIdwithaddBook()andcollectFine(); Book hasisbn,titleandavailable; Loan hasloanId,issueDate,dueDate,returnDateandfine; the multiplicities are Library 1 to * Book, Member 1 to 0..* Loan and Loan * to 1 Book. - The fine is days late x rate per day, so a book returned 5 days late at Rs 2 per day costs 5 x 2 = Rs 10.
Steps.
Step 1: Customer inserts the card; ATM reads it and sends the PIN to the Bank, which authenticates it.
Step 2: Withdraw: ATM asks the Bank to check the balance; if enough, Account.debit() runs, cash is dispensed and a Withdrawal is logged.
Step 3: Deposit: Customer inserts cash; Account.credit() runs and a Deposit is logged.
Step 4: Check Balance: ATM asks the Bank, which reads Account.balance and the ATM displays it.
Step 5: Transfer: source Account.debit() then target Account.credit() run as one Transfer.
Step 6: ATM prints the receipt and returns the card.
Example.
class Account { double balance;
synchronized void debit(double a) { if (a <= balance) balance -= a; } // one thread at a time
synchronized void credit(double a) { balance += a; } }
abstract class Transaction { Account acc; double amt;
Transaction(Account acc, double amt) { this.acc = acc; this.amt = amt; }
abstract void execute(); }
class Withdrawal extends Transaction { Withdrawal(Account a, double m) { super(a, m); }
void execute() { acc.debit(amt); } }
class Deposit extends Transaction { Deposit(Account a, double m) { super(a, m); }
void execute() { acc.credit(amt); } }
new Withdrawal(acc, 1000).execute(); // runs Withdrawal's version
class Loan { Book book; LocalDate due, ret; double rate = 2;
double fine() { long d = ChronoUnit.DAYS.between(due, ret);
return d > 0 ? d * rate : 0; } } // 5 days late -> 10.0
Answer frame. Open with the system and its purpose; draw the class diagram with attributes, methods and multiplicity, then the use-case diagram; give the interactions in order (withdraw, deposit, balance, transfer; or issue, return, fine, search); close with encapsulation, inheritance, polymorphism and abstraction.
Asked: [7 marks] (Dec 2023) Explain library management system using object-oriented diagram. Asked: [7 marks] (Jun 2023) Explain ATM system using object-oriented diagram.
Last-minute revision
- Throwable is the root of Error and Exception; RuntimeException is unchecked.
- try holds risky code, catch handles it, finally always runs, throw raises, and throws declares.
- ArrayIndexOutOfBounds: index < 0 or >= length; NullPointer: method called on null.
- Java strings are immutable and pooled; use StringBuilder and
equals(). newcalls the constructor anddeletethe destructor; arrays needdelete[].- A pure virtual function is
virtual void f() = 0;and makes the class abstract. - Thread states: New, Runnable, Running, Blocked, Terminated;
synchronizedstops races. - Collections: List, Set, Queue, Map (not a Collection); ConcurrentHashMap is thread-safe.
- Library fine = days late x rate per day.
Memory hooks
- TCF: try, catch, finally, always in that order.
- Throw raises, Throws declares, Catch cleans.
- NBRTT for thread states: New, Blocked, Runnable, Terminated, Timed waiting.
- LSM: List, Set, Map.
- RMA for a message: Receiver, Method, Arguments.
Coverage checklist
- Strings: Dec 2024 strings in OOP.
- Exceptional handling: Java exceptions (Dec 2023, Jun 2023, Dec 2024, Jun 2025), short notes exceptions, new/delete, polymorphism (Nov 2022, Jun 2024), Array Index and Null Pointer (Jun 2023, Jun 2024), throw vs catch (Jun 2025), templates (Nov 2022), exceptions and multithreading (Nov 2022, Dec 2025).
- Introduction of Multi-threading and Data collections: multithreading benefits (Dec 2023), collection, message passing, static polymorphism (Dec 2023), design question (Dec 2024), short notes incl. virtual friend, abstract classes, type conversion (Jun 2025), collections case study (Dec 2025).
- Case study like: ATM, Library management system: library (Dec 2023), ATM (Jun 2023).