Foundation: the core computer science a fresher is asked about · Chapter 2
Interfaces, composition and the questions that follow
About this chapter
The four pillars are rarely the end of a fresher round, and the follow-up questions are where answers fall apart. Using one pharmacy stock system, this chapter covers what an interface promises, why a store should hold a printer rather than be one, what equal and hash mean for objects, how Python picks between two parents that define the same method, and what immutable means. It ends with how to say each answer aloud.
Who it is for: Students and freshers who know classes and inheritance in Python and are preparing for the follow-up questions interviewers ask about object-oriented design.
You will be able to
- Explain an interface as a promise and write one with an abstract base class in Python.
- Choose composition over inheritance and justify it with a has-a sentence.
- Say when two objects are equal and why equal objects need matching hashes.
- Explain which parent wins in multiple inheritance and how Python decides.
- Define immutability and give a reason to want it.
The key idea: Inheritance says what a thing is, and composition says what a thing has. Many design mistakes in early code come from using the first where the second was meant, and the quickest test is to say the relationship aloud and hear whether it is true.
In this chapter
- Where chapter 1 left the counter
- An interface is a promise written down
- Composition: a store has printers, it is not one
- Equality, hashing and the two kinds of method
- Multiple inheritance and the order of lookup
- Immutability and why a tuple is not a class
- How to answer the class questions in a round
Locked: needs a plan
You can read chapter 1 free. Chapter 2 is the next one.
What you can do now, free: re-read chapter 1 and try its problems.
What a plan gives you: all chapters of the track you pick and their checks. Starter is ₹229 a month or ₹1,699 a year. It renews until you cancel, and you can cancel any time. Paid plans open at launch.
Why is this locked?
Chapter 1 of every track is free, so you can see how the track reads. The rest of a track and its checks come with a plan, or open as you pass each chapter's check.
The price is for one period, a month or a year. A plan renews until you cancel.