OOP Flashcards
What is Object-Oriented Programming?
A programming paradigm based on the concept of “objects” which can contain data (attributes) and code (methods), and which interact through defined interfaces.
What are the four main principles of OOP?
Encapsulation
Abstraction
Inheritance
Polymorphism
What is encapsulation in OOP?
The bundling of data with the methods that operate on that data, and restricting direct access to some of an object’s components.
How is encapsulation typically implemented?
Through access modifiers (private, protected, public) and getter/setter methods.
What is inheritance in OOP?
A mechanism where a new class (child) is derived from an existing class (parent), inheriting its attributes and methods.
What are the benefits of inheritance?
Code reusability
Method overriding (polymorphism)
Creating hierarchical classifications
What is polymorphism in OOP?
The ability of objects to take on many forms, typically through method overriding (runtime polymorphism) or method overloading (compile-time polymorphism).
What’s the difference between method overloading and overriding?
Overloading: Same method name, different parameters (compile-time)
Overriding: Same method signature in child class (runtime)
What is abstraction in OOP?
Hiding complex implementation details and showing only essential features of an object.
How is abstraction implemented?
Through abstract classes and interfaces that define what an object does without specifying how it does it.
What is a class in OOP?
A blueprint or template for creating objects, defining their properties and behaviors.
What is an object in OOP?
An instance of a class that contains data (attributes) and behavior (methods).
What is an interface?
A completely abstract class that defines a contract (methods) that implementing classes must follow.
What is an abstract class?
A class that cannot be instantiated and may contain both abstract and concrete methods.
What is composition in OOP?
A design principle where a class contains objects of other classes (has-a relationship) rather than inheriting from them.
What is the difference between “is-a” and “has-a” relationships?
“Is-a” = inheritance (a Car is a Vehicle)
“Has-a” = composition (a Car has an Engine)
What is the SOLID principle in OOP?
Five design principles:
Single Responsibility
Open/Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
What is the Single Responsibility Principle?
A class should have only one reason to change, meaning it should have only one responsibility or job.
Example:
❌ Bad: A User class that handles user data and saves to a database.
✅ Good: Separate User (data) and UserRepository (database operations).
What does the Open/Closed Principle state?
Software entities (classes, modules, functions) should be open for extension but closed for modification.
Example:
❌ Bad: Modifying an existing PaymentProcessor class to add new payment methods.
✅ Good: Using interfaces (CreditCardPayment, PayPalPayment) to extend functionality without changing existing code.
What does the Liskov Substitution Principle state?
Subtypes must be substitutable for their base types without altering the correctness of the program.
Example:
❌ Bad: A Square class inheriting from Rectangle but changing setWidth() behavior, breaking expectations.
✅ Good: Ensuring derived classes do not violate base class behavior.
What does the Interface Segregation Principle state?
Clients should not be forced to depend on interfaces they do not use. Split large interfaces into smaller, specific ones.
Example:
❌ Bad: A single Worker interface with work(), eat(), and sleep().
✅ Good: Separate Workable, Eatable, and Sleepable interfaces.
What does the Dependency Inversion Principle state?
High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.
Example:
❌ Bad: A ReportGenerator class directly depending on MySQLDatabase.
✅ Good: ReportGenerator depending on a Database interface, allowing any database implementation.