IKM Object-Oriented Programming Principles 2 — Questions and Answers
Question 1: What is encapsulation in OOP?
- Inheriting methods
- Bundling data and methods while restricting access to internal state (Correct answer)
- Creating multiple objects
- Using interfaces
Correct answer: Bundling data and methods while restricting access to internal state
Encapsulation bundles data and methods within a class, hiding internal implementation.
Access modifiers (private, protected, public) control visibility. Private fields accessed through getters/setters allow validation and future implementation changes without breaking client code.
Question 2: What is the difference between overloading and overriding?
- Overloading changes return types
- Overloading uses different parameters in same class; overriding redefines a parent method in subclass (Correct answer)
- They are the same
- Overloading is compile-time only
Correct answer: Overloading uses different parameters in same class; overriding redefines a parent method in subclass
Overloading provides same-name methods with different parameters; overriding redefines inherited methods.
Overloading is compile-time polymorphism. Overriding is runtime polymorphism where a subclass provides specific implementation of a parent method with the same signature.
Question 3: What is an abstract class?
- Class with no methods
- Class that cannot be instantiated and may contain abstract methods (Correct answer)
- Class with only static methods
- Class that auto garbage collects
Correct answer: Class that cannot be instantiated and may contain abstract methods
Abstract classes cannot be instantiated and may have abstract methods subclasses must implement.
Abstract classes can have both abstract methods (no body) and concrete methods. They can have instance variables and constructors. A class can extend only one abstract class but implement multiple interfaces.
Question 4: What is the Liskov Substitution Principle?
- Single responsibility
- Subclass objects should be replaceable for superclass objects without breaking the program (Correct answer)
- Depend on abstractions
- Open for extension
Correct answer: Subclass objects should be replaceable for superclass objects without breaking the program
LSP states subclass objects must work wherever superclass objects are expected.
A classic violation: Square extends Rectangle but setting width changes both dimensions. LSP requires preconditions cannot be strengthened and postconditions cannot be weakened in subtypes.
Question 5: What is composition over inheritance?
- Using constructors over factories
- Favoring has-a relationships over is-a relationships for code reuse (Correct answer)
- Composing interfaces
- Multiple inheritance
Correct answer: Favoring has-a relationships over is-a relationships for code reuse
Composition uses contained objects for behavior rather than extending classes.
Inheritance creates tight coupling. Composition delegates to contained objects that can be swapped at runtime. The Strategy pattern formalizes this. Go and Rust use composition exclusively.
Question 6: What is the purpose of an interface in OOP?
- Store global variables
- Define a contract of methods implementing classes must provide (Correct answer)
- Manage memory
- Handle logging
Correct answer: Define a contract of methods implementing classes must provide
An interface defines method signatures that implementing classes must provide.
A class can implement multiple interfaces. Interfaces support Dependency Inversion: high-level modules depend on abstractions. The Interface Segregation Principle advises keeping interfaces small.
What is encapsulation in OOP?