Skip to content

Oop Principles

Master Oop Principles with 50 free flashcards. Study using spaced repetition and focus mode for effective learning in Programming.

🎓 50 cards ⏱️ ~25 min Advanced
Study Full Deck →
Share: 𝕏 Twitter LinkedIn WhatsApp

🎯 What You'll Learn

Preview Questions

12 shown

What is encapsulation in OOP?

Show ▼

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

What is inheritance in OOP?

Show ▼

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

What is polymorphism in OOP?

Show ▼

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.

What is abstraction in OOP?

Show ▼

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.

What is the Single Responsibility Principle (SRP)?

Show ▼

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.

What is the Open/Closed Principle (OCP)?

Show ▼

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

What is the Liskov Substitution Principle (LSP)?

Show ▼

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.

What is the Interface Segregation Principle (ISP)?

Show ▼

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.

What is the Dependency Inversion Principle (DIP)?

Show ▼

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.

What is composition vs inheritance?

Show ▼

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.

Why is composition often preferred over inheritance?

Show ▼

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.

What is the difference between an interface and an abstract class?

Show ▼

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

🎓 Start studying Oop Principles

🎮 Study Modes Available

🔄

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

Related Topics in Programming

📖 Learning Resources