A network developer is building an application that needs to retrieve detailed information about network devices from a Cisco DNA Center API. The API requires authentication using a token obtained from a prior login request. The application design uses an object-oriented approach where a `DNACClient` class handles all API interactions. Which design principle is being primarily applied when the `DNACClient` class encapsulates the token management (obtaining, storing, refreshing) internally, exposing only high-level methods like `get_device_info(device_id)`?
- AEncapsulation
- BInheritance
- CPolymorphism
- DAbstraction
Show answer & explanationAnswer & explanation
Correct answer: A. Encapsulation
Encapsulation is the bundling of data (like the authentication token) and methods (like obtaining and refreshing the token) that operate on the data into a single unit (the `DNACClient` class), and restricting direct access to some of the object's components. This hides the internal complexities of token management from the user of the `DNACClient` class.
Why the other options are wrong
- B. Inheritance is about creating new classes from existing ones, inheriting their attributes and methods, which is not the primary principle described here.
- C. Polymorphism is the ability of an object to take on many forms, allowing methods to behave differently based on the object type, which is unrelated to hiding token management details.
- D. Abstraction focuses on showing only essential information and hiding complex implementation details. While related to encapsulation, the scenario specifically describes the bundling and hiding of internal data (token) and its operations, which is the core of encapsulation. Abstraction is the *result* of applying encapsulation and other principles.
Encapsulation (OOP)
Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP). It refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (a class), and restricting direct access to some of the object's components, typically achieved through access modifiers or by simply not exposing internal details.
- Bundles data and methods into a single unit (class)
- Restricts direct access to internal state
- Hides implementation details from the outside world
- Promotes data integrity and maintainability
Memory trick: Encapsulation 'capsules' data and functions, keeping secrets safe.