Master Oop Principles with 50 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.
Encapsulation is the bundling of data and methods that operate on that data into a single unit (class), while restricting direct access to some components.
Key benefits:Hides internal state and implementation detailsExposes only a controlled public interfaceProtects object integrity by preventing unintended modification
Inheritance allows a child class (subclass) to acquire the properties and behaviors of a parent class (superclass).
Example in Java:class Dog extends Animal { }
Benefits:Promotes code reuseEstablishes an is-a relationshipEnables polymorphic behavior
Polymorphism means "many forms" — the ability of an object to take on different behaviors depending on its type.
Two main types:Compile-time (static): method overloadingRun-time (dynamic): method overriding via inheritanceExample: a Shape.draw() method behaves differently for Circle vs Rectangle.
Abstraction is the process of hiding complex implementation details and exposing only the essential features of an object.
Achieved through:Abstract classes — partial implementationInterfaces — pure contract, no implementationExample: a Vehicle interface exposes start() and stop() without revealing engine internals.
The S in SOLID. A class should have only one reason to change, meaning it should have only one job or responsibility.
Violation example: a UserService class that handles authentication, email sending, and database queries.
Fix: split into AuthService, EmailService, and UserRepository.
The O in SOLID. Software entities should be open for extension but closed for modification.
In practice:Add new behavior by creating new classes or methodsDon't modify existing, tested codeUse interfaces, abstract classes, and strategy patterns to enable extension
The L in SOLID. Objects of a superclass should be replaceable with objects of a subclass without breaking the program.
Classic violation: Square extends Rectangle — setting width on a Square unexpectedly changes height.
Rule: subclasses must honor the behavioral contract of the parent class.
The I in SOLID. Clients should not be forced to depend on interfaces they do not use.
Violation: a fat Worker interface with work(), eat(), sleep() — a Robot doesn't eat or sleep.
Fix: split into Workable, Eatable, and Sleepable interfaces.
The D in SOLID. High-level modules should not depend on low-level modules — both should depend on abstractions.
Example:class OrderService { private PaymentGateway gateway; }
Here PaymentGateway is an interface, not a concrete class like StripePayment. This enables easy swapping of implementations.
Composition models a has-a relationship; inheritance models an is-a relationship.
Composition example: Car has an Engine.
Inheritance example: Dog is an Animal.
The principle "favor composition over inheritance" encourages using composition for code reuse because it provides greater flexibility and avoids tight coupling.
Composition is preferred because:Loose coupling — components can be swapped at runtimeNo fragile base class problem — changes to a parent don't break childrenGreater flexibility — combine behaviors from multiple sourcesAvoids deep hierarchies that are hard to understandUse inheritance only when a true is-a relationship exists.
Interface:Defines a contract with no state (traditionally)A class can implement multiple interfacesAll methods are implicitly public abstract (in Java)Abstract class:Can have state (fields) and concrete methodsA class can extend only one abstract classCan define constructors and access modifiers
Flashcards
Flip to reveal
Focus Mode
Spaced repetition
Multiple Choice
Test your knowledge
Type Answer
Active recall
Learn Mode
Multi-round mastery
Match Game
Memory challenge