All questions
Showing of 79What are the four pillars of object-oriented programming?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Encapsulation, abstraction, inheritance and polymorphism are the four, and each buys something different. Encapsulation keeps an object's data and the code that changes it together, behind a guarded door. Abstraction shows what an object does and hides how it does it. Inheritance lets one class build on another. Polymorphism lets one piece of code work with several types.
These are not four independent features. Encapsulation and abstraction are two views of the same boundary. One hides state, the other hides steps. Inheritance is only one route to polymorphism, and often not the best one.
Treat them as tools, not as goals. Applying all four to every class produces deep hierarchies and wrapper types that buy nothing. The real payoff is narrow and measurable. A change lands in one file instead of twenty, and a new case plugs in without touching old code.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is encapsulation, and what does hiding an object's state actually buy you?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
An object owns its data, and outside code reaches that data only through methods the object controls. Fields stay private, and the class decides which changes are legal. The private keyword is the mechanism; the discipline is the point.
Hiding state buys three things. You enforce invariants in one place, so an account refuses a withdrawal that goes below zero and no caller can skip that check. You swap the internal representation, a list for a map, without touching callers. And when a balance is wrong, the suspect list is one class, not the whole codebase.
The cost is ceremony. Not every type needs a wall around it. A plain data holder passed between two functions gains nothing from private fields and pass-through accessors, and the extra layer just makes the code longer to read.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is abstraction, and what techniques do you use to achieve it?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Abstraction is deciding what to leave out. You name an operation by its intent and hide the steps behind that name. A caller writes save(order) and never learns whether that is a SQL insert, a file write or an HTTP call.
The techniques are ordinary. Interfaces state a contract with no implementation. Abstract classes fix a shape and leave gaps for subclasses. Ordinary methods give one name to a block of steps. Access modifiers back all of this up by keeping the internals out of reach.
A wrong abstraction costs more than none at all. If the contract you picked does not match how callers use the thing, every caller writes a workaround. Flags and escape hatches follow. Duplicated code is easier to fix later than a bad abstraction everyone depends on.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is inheritance for, and what types of inheritance exist?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Inheritance lets one class take another's fields and behaviour as a starting point, then add to them or replace parts. It gives two things at once: code reuse, and a subtype relationship, so a dog can be passed anywhere an animal is expected. The second is the part that matters.
Textbooks name the shapes by how the arrows point:
- Single: one class extends one parent.
- Multilevel: a chain, where a child is itself a parent to something else.
- Hierarchical: several classes extend the same parent.
- Multiple: one class extends more than one parent, which most languages allow for interfaces but not classes.
The split worth remembering is implementation inheritance versus interface inheritance. Taking a parent's code ties you to its internals, so its next change can break you. Taking only its contract does not. Every subclass is also a standing promise to keep behaving like the parent, which is easy to make and hard to keep.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is polymorphism, and what types of polymorphism do languages support?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Code written against one name can work with many different types, and the right version runs automatically. A loop calling shape.area() gets the circle's formula for circles and the square's for squares. The calling code never branches on type.
Languages support several flavours:
- Subtype polymorphism: a subclass or implementation supplies its own version, and the object picks at runtime.
- Ad-hoc polymorphism: one name, several parameter lists, resolved when the code is compiled. Operator overloading is the same idea.
- Parametric polymorphism: generics, where one
List<T>works for every element type. - Duck typing: in dynamic languages, any object with the right method fits, with no declared type.
The gain is that adding a type does not mean editing existing branches, so old code stays untouched. The cost is indirection. Reading a call site no longer tells you which method actually runs, and a debugger or a stack trace becomes the only honest answer.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the difference between composition and inheritance?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Composition means one object holds another and asks it to do work. Inheritance means one class is a specialised kind of another and receives its members automatically. A car has an engine; an electric car is a car.
Three differences bite in practice. Inheritance is fixed when the code is written, while a composed part can be swapped at runtime. A subclass can see protected members, while a holder sees only the public surface. A subclass inherits the whole parent interface, wanted or not.
Both reuse code, but only inheritance also declares substitutability. That promise is what lets a subclass be passed where the parent is expected, and it is the part you cannot take back later. If all you want is reuse, composition asks for less and gives back more room to change.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
How does an abstract class differ from an interface?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
An interface and an abstract class can both ship working code today. The real dividing lines are state, constructors and the single-inheritance slot.
Java 8 added default and static methods to interfaces. C# 8 added default interface implementations.
interface Greeter {
String name();
default String greet() { return "Hi, " + name(); }
}
Interfaces cannot hold instance fields (per-object data) or define constructors. An abstract class holds both, plus protected helpers subclasses share.
| Feature | Interface | Abstract class |
|---|---|---|
| Method bodies | Yes, default and static | Yes |
| Instance fields | No | Yes |
| Constructors | No | Yes |
| Allowed per class | Many | One |
A class can implement many interfaces but extend only one abstract class. That makes an interface cheap to add to a type that already has a parent. An abstract class can carry state and enforce an order of steps.
The choice comes down to whether subclasses genuinely share code, or only share a capability that callers depend on.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What are the SOLID principles, and why do teams bother with them?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Five design rules, one per letter, all aimed at code that people keep changing.
- Single responsibility: a class should have one reason to change.
- Open/closed: you should be able to add behaviour without editing code that already works.
- Liskov substitution: a subtype must work wherever its base type does, with no surprises.
- Interface segregation: several small contracts beat one fat contract that forces empty methods.
- Dependency inversion: high-level code should depend on abstractions, not on concrete classes.
Teams bother because each letter names a specific pain they have already felt. Shotgun edits across five files for one change. An if chain that grows with every new feature. A subclass that throws from a method it inherited. Stubs full of empty methods. Logic that cannot run without a real database. Applied blindly, though, SOLID produces a class per line of logic, which is its own mess.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is the Single Responsibility Principle, and what does it ask of a class?
What is the Open/Closed Principle, and what is a real example of it?
What does the Liskov Substitution Principle ask of a subclass, and why?
What is the Interface Segregation Principle trying to prevent?
What is dependency injection, and how does it differ from creating objects yourself?
What are the three forms of dependency injection, and how do they differ?
What is a dependency injection container, and what work does it take over?
What do coupling and cohesion mean, and why aim for low coupling and high cohesion?
What is the difference between reference equality and value equality?
When you override equals, what other method must you override, and why?
What must a correct equals method guarantee, including when it is given null?
What is an immutable class, and how do you recognise one?
What are the rules a method must follow to be a valid overload?
Give a real-world example of abstraction and one of encapsulation
What do public, private and protected mean, and when would you use each?
What is the difference between a class and an object?
What are the SOLID principles and why are they important in software development?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
SOLID is an acronym representing five fundamental design principles for object-oriented programming that promote clean, maintainable, and extensible code:
- S - Single Responsibility Principle: A class should have only one reason to change
- O - Open/Closed Principle: Software entities should be open for extension but closed for modification
- L - Liskov Substitution Principle: Objects should be replaceable with instances of their subtypes
- I - Interface Segregation Principle: Clients should not depend on interfaces they don't use
- D - Dependency Inversion Principle: Depend on abstractions, not concretions
Benefits of following SOLID principles:
- Maintainability: Changes are localized and predictable
- Flexibility: Easy to extend and modify existing functionality
- Testability: Components can be tested in isolation
- Reusability: Well-designed components can be reused across different contexts
- Reduced coupling: Components are loosely connected and independently changeable
- Code clarity: Intent and structure are clearer and easier to understand
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
Explain the Dependency Inversion Principle.
Why prefer composition over inheritance, and what does each approach cost you?
Should a class hierarchy start from an interface or an abstract class?
Why do most languages allow many interfaces but only one base class?
What is the diamond problem, and how do languages resolve it?
What are the limits of inheritance, including the fragile base class problem?
What is the difference between method overloading and method overriding?
Can a static method be overridden, or is it only hidden?
What is compile-time polymorphism, and how does it differ from runtime polymorphism?
Which methods can a subclass never override, and why not?
Can an overriding method declare a different return type than the original?
Why is calling a static method through an instance considered bad practice?
How is encapsulation different from abstraction when they look so similar?
Describe a class that violates the Single Responsibility Principle and how you would split it
A subclass cannot honour a method it inherits - why does that break LSP?
An interface has ten methods but a class needs two - how do you fix that?
What does the Dependency Inversion Principle mean, and can you give an example?
How do you add a new payment type without editing the existing classes?
What is inversion of control, and how does dependency injection relate to it?
What are the downsides and hidden costs of dependency injection?
How do you reduce coupling between two classes that know too much about each other?
Can two objects that are not equal share the same hash code?
Should an entity's id field take part in its equals method?
What steps turn an ordinary class into a genuinely immutable one?
Are immutable objects automatically thread safe, and what makes them so?
Why do immutable objects make better hash map keys than mutable ones?
When you pass an object to a method, is it by value or reference?
What is the difference between a shallow copy and a deep copy of an object?
What is the Singleton pattern, and what problems does it cause?
In what order do field initialisers and constructors run when you create a subclass?
How do SOLID principles relate to code maintainability and testability?
What design patterns help achieve the Open/Closed Principle?
What are the key rules for maintaining LSP compliance?
How does ISP relate to cohesion and coupling?
What's the difference between ISP and SRP?
What is Dependency Injection and how does it relate to DIP?
How do you keep a class immutable when one of its fields is mutable?
Should equals accept subclasses in its type check, or require an exact class match?
What breaks when equality and ordering comparison disagree about two objects?
Do getters and setters really give you encapsulation, or just expose the fields?
A class that fetches, processes and formats a report - which SOLID principles break?
A method switching over a type code - which SOLID principles does that violate?
How does a service locator compare with dependency injection?
What are the benefits and potential drawbacks of strictly following SRP?
How do you balance OCP with YAGNI (You Aren't Gonna Need It) principle?
How do you identify and fix LSP violations in existing code?
How do you apply DIP in scenarios without a DI container?
What are the trade-offs of applying DIP extensively?
How do SOLID principles interact with each other? Can following one principle violate another?
How do you introduce SOLID principles to a legacy codebase?
How do SOLID principles apply to microservices architecture?
What metrics or code smells indicate SOLID principle violations?
How do you balance SOLID principles with performance requirements?
Can you provide an example of refactoring a complex class to follow all SOLID principles?
This answer is part of Pro.
The full written answer, with the trade-offs and follow-ups an interviewer will probe.
No matches
Try a different filter or search term.
Object-Oriented Programming cheatsheet
- 30-second mental model01
- Choosing the mechanism02
- SOLID03
- Dependency injection04
- Polymorphism and dispatch05
- Equality and hashing06
- Immutability07
- Passing objects08
- Rules of thumb09
- + 3 more inside
70 of 79 Object-Oriented Programming answers are in Pro.
Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.
- Full answers + code
- AI explanations, simpler or deeper
- 1,000 AI credits / month
- Cancel anytime
Change topic
Pick a different technology or stack. Your current topic stays put until you choose a new one.
MEAN
MongoDB, Express, Angular, Node.jsMERN
MongoDB, Express, React, Node.jsDjango
Python Full-Stack DevelopmentRuby on Rails
Convention over ConfigurationServerless on AWS
Serverless Architecture on AWSInterviewers also test these - they're common to every stack, whichever one you picked above.
Flutter Mobile
Flutter Cross-Platform Mobile DevelopmentInterviewers also test these - they're common to every stack, whichever one you picked above.
Spring Boot
Enterprise Java Development.NET
Microsoft EcosystemVue
Vue.js, Vite, TypeScript, Tailwind, Node.jsGo Backend
Golang, gRPC, PostgreSQL, Redis, RabbitMQInterviewers also test these - they're common to every stack, whichever one you picked above.
FastAPI
Python, FastAPI, SQLAlchemy, PostgreSQLReact Native
React, TypeScript, Redux, FirebaseiOS Native
Swift, SwiftUI, UIKit, FirebaseAndroid Native
Java, Jetpack Compose, FirebaseDevOps / Platform
Docker, Kubernetes, Terraform, CI/CDInterviewers also test these - they're common to every stack, whichever one you picked above.
AI Engineer
LLMs, RAG, Agents, EvalsAI-Powered Developer
Claude Code, Copilot, Agentic WorkflowsCore SWE Interview Prep
Data structures, algorithms, OS, concurrency, networking, gitInterviewers also test these - they're common to every stack, whichever one you picked above.
Interviewers also test these - they're common to every stack, whichever one you picked above.