Object-Oriented Programming Principles Flashcards
6 cards from real IKM practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Object-Oriented Programming Principles flashcards as text
A developer creates a `Vehicle` class and then defines `Car`, `Truck`, and `Motorcycle` classes that each derive from the `Vehicle` class. Each of these new classes can now reuse the common methods and properties of the `Vehicle` class. Which object-oriented principle does this scenario best exemplify?
Answer: Inheritance
Inheritance is the mechanism where a new class (subclass or derived class) acquires the properties and behaviors of an existing class (superclass or base class). In this case, `Car`, `Truck`, and `Motorcycle` inherit from `Vehicle`, promoting code reuse.
Which of the following best describes the principle of Encapsulation in object-oriented programming?
Answer: Bundling the data (attributes) and the methods (functions) that operate on the data into a single unit, or class, and restricting access to the internal state.
Encapsulation is the practice of bundling data and the methods that operate on that data within a single unit, like a class. It also involves restricting direct access to an object's internal state, which is a concept known as data hiding.
A program uses a `Shape` class with a method called `draw()`. Several other classes, such as `Circle`, `Square`, and `Triangle`, inherit from `Shape` and provide their own specific implementation of the `draw()` method. When the program calls the `draw()` method on a list of `Shape` objects, the correct `draw()` implementation is executed for each specific shape. This is an example of:
Answer: Polymorphism
Polymorphism, which means 'many forms', allows objects of different classes to be treated as objects of a common superclass. It enables a single interface (like the `draw()` method) to be used for a general class of actions, with each specific subclass providing its own implementation.
Consider a `DatabaseConnection` class. The user of this class only needs to know about methods like `connect()`, `query()`, and `disconnect()`. The complex internal details, such as managing network sockets, handling authentication protocols, and managing data buffers, are hidden from the user. Which OOP principle is most evident here?
Answer: Abstraction
Abstraction is the concept of hiding complex implementation details and showing only the essential features of the object. It focuses on what an object does rather than how it does it. The user interacts with a simplified interface without needing to understand the underlying complexity.
In object-oriented design, what is the primary purpose of defining a class with `private` attributes and `public` methods (getters and setters)?
Answer: To enforce encapsulation by controlling access to the object's internal state.
Making attributes `private` hides them from outside access. Providing `public` methods (getters and setters) creates a controlled interface for interacting with that data. This is the fundamental mechanism for enforcing encapsulation and data hiding, preventing direct, uncontrolled modification of an object's state.
Which statement accurately distinguishes Abstraction from Encapsulation?
Answer: Abstraction focuses on showing essential features (the 'what'), while Encapsulation is the mechanism of bundling data and hiding the implementation details (the 'how').
Abstraction is a design-level concept that focuses on showing only necessary details to the outside world (what an object does). Encapsulation is an implementation-level mechanism that bundles data with the methods that operate on it and hides the internal state to protect it from outside interference (how it does it).