31. What is SOLID in OOP?
SOLID is a set of 5 design principles for writing maintainable, scalable OOP code. It stands for:
S โ Single Responsibility
O โ Open/Closed
L โ Liskov Substitution
I โ Interface Segregation
D โ Dependency Inversion
32. Explain each SOLID principle briefly:
โข Single Responsibility โ A class should do one thing only.
โข Open/Closed โ Classes should be open for extension, but closed for modification.
โข Liskov Substitution โ Subclasses should replace their parent classes without breaking functionality.
โข Interface Segregation โ Prefer small, specific interfaces over large ones.
โข Dependency Inversion โ Depend on abstractions, not concrete classes.
33. What is Liskov Substitution Principle?
If a class S is a subclass of class T, objects of type T should be replaceable with objects of type S without affecting the program.
Example: A Bird base class with a
fly() method may break if Penguin inherits it (Penguins can't fly). So, design must respect capabilities.34. What is Dependency Inversion Principle?
High-level modules should not depend on low-level modules. Both should depend on abstractions.
Example: A service class should depend on an interface, not a specific implementation.
35. What is object slicing?
Occurs when an object of a derived class is assigned to a base class variable โ the extra properties of the derived class are "sliced off."
Example: C++ object slicing when passing by value.
36. What are getters and setters?
Special methods used to get and set values of private variables in a class.
They support encapsulation and validation.
def get_name(self): return self._name
def set_name(self, name): self._name = name
37. What is a virtual function?
A function declared in the base class and overridden in the derived class, using the
virtual keyword (in C++). Enables run-time polymorphism.38. What is early binding vs late binding?
โข Early Binding (Static): Method call is resolved at compile time (e.g., method overloading).
โข Late Binding (Dynamic): Method call is resolved at run-time (e.g., method overriding).
39. What is dynamic dispatch?
Itโs the process where the method to be invoked is determined at runtime based on the objectโs actual type โ used in method overriding (late binding).
40. What is a pure virtual function?
A virtual function with no implementation in the base class โ makes the class abstract.
Syntax (C++):
virtual void draw() = 0;
๐ฌ Double Tap โฅ๏ธ for Part-5