ANSWER KEY: Non-Code examples of the pillars of OOP

Note - I think that there are valid arguments to be made that a few of these illustrate more than one pillar. To decide wthich one BEST is illustrated I considere the main focus of the scenario.

 

For each of the following scenarios, identify which of the four pillars of OOP [abstraction, encapsulation, inheritance, and polymorphism] the scenario best illustrates.

  1. You pull out your wallet and pick which credit card to use to pay the bill at the grocery store.
    • The scenario best illustrates the Polymorphism pillar of Object-Oriented Programming (OOP). Polymorphism allows a single action to operate on different types of objects.

      In this analogy:

      • You are the method or function.
      • The wallet represents the interface.
      • The different credit cards are the different objects.

      When you choose a credit card to pay the bill, you are performing the same action (paying the bill) but with different objects (different credit cards). Each credit card might have different details (e.g., different banks, credit limits), but they all serve the same purpose in this context.

      This ability to use different objects interchangeably while performing the same operation is a key aspect of polymorphism.


  2. You are creating an exam. You open a copy of last year's exam and make a copy. You then go in and change several questions to better fit what you did this semester.
    • The scenario best illustrates the Inheritance pillar of Object-Oriented Programming (OOP). Inheritance allows a new class to inherit properties and methods from an existing class, enabling code reuse and the creation of a hierarchical relationship between classes.

      In this analogy:

      • Last year’s exam represents the parent class.
      • This year’s exam represents the child class that inherits from the parent class.
      • Changing several questions is akin to overriding or extending the inherited properties to better fit the current context.

      By copying last year’s exam and modifying it, you are reusing the existing structure and content (inheriting) while making necessary adjustments (extending or overriding) to suit the new requirements.


  3. You go to the store and buy a pack of invitations. You add your name, the date, and the location on each card and mail several to your friends.
    • The scenario best illustrates the Inheritance pillar of Object-Oriented Programming (OOP). Inheritance allows a new class to inherit properties and methods from an existing class, enabling code reuse and extension.

      In this analogy:

      • The pack of invitations represents the parent class, providing a basic structure and template.
      • Adding your name, the date, and the location represents the child class, which inherits the basic structure from the parent class and adds or modifies specific details to suit the current context.

      By using the pre-made invitations and customizing them with your details, you are reusing the existing template (inheriting) and extending it with new information to fit your needs.


  4. You ask your child how they did on their spelling test and they tell you that they got a perfect score.
    • The scenario best illustrates the Encapsulation pillar of Object-Oriented Programming (OOP). Encapsulation involves bundling the data and methods that operate on the data into a single unit, and restricting access to some of the object’s components.

      In this analogy:

      • Your child represents the object.
      • The spelling test score is the data.
      • The act of asking and receiving the score is the method that allows controlled access to the data.

      Your child holds the information about their test score (data) and only shares it with you when you ask (method). This encapsulation ensures that the data is not directly accessible without going through the appropriate method (asking your child).


  5. Your child asks you to make them their favorite sandwich. You do.
    • The scenario best illustrates the Abstraction pillar of Object-Oriented Programming (OOP). Abstraction involves hiding the complex implementation details and showing only the necessary features.

      In this analogy:

      • Your child represents the user who requests a specific action (making their favorite sandwich).
      • You represent the method or function that performs the action.
      • Making the sandwich involves various steps and details (choosing ingredients, assembling the sandwich), but these complexities are hidden from your child.

      Your child doesn’t need to know the detailed steps involved in making the sandwich; they only care about the final result. This is similar to how abstraction in OOP hides the complex implementation details and provides a simple interface for the user.


  6. You are holding parent/teacher conferences. When each parent comes in you talk with them about how their student is doing in the class and what they, as a parent, have agreed to provide for the fall holiday party.
    • The scenario best illustrates the Polymorphism pillar of Object-Oriented Programming (OOP). Polymorphism allows the same operation to behave differently on different classes.

      In this analogy:

      • You are the method or function.
      • Each parent represents a different object.
      • Talking about the student’s progress and the fall holiday party represents the operation.

      When each parent comes in, you perform the same general operation (discussing the student’s progress and the holiday party), but the specifics of the conversation vary depending on the particular parent and student. This is similar to how polymorphism allows a single method to operate differently based on the object it is acting upon.


  7. You go to the car dealership to have your oil-changed and new wiper blades installed.
    • The scenario best illustrates the Abstraction pillar of Object-Oriented Programming (OOP). Abstraction involves hiding the complex implementation details and showing only the necessary features.

      In this analogy:

      • You are the user who needs specific services (oil change and new wiper blades).
      • The car dealership represents the method or function that performs these services.
      • The services (oil change and wiper blade installation) involve various detailed steps and processes that are hidden from you.

      As a customer, you don’t need to know the intricate details of how the oil is changed or how the wiper blades are installed. You simply request the services, and the dealership takes care of the rest. This is similar to how abstraction in OOP hides the complex implementation details and provides a simple interface for the user.


  8. You use the ATM to check on the balance in your checking account.
    • The scenario best illustrates the Encapsulation pillar of Object-Oriented Programming (OOP). Encapsulation involves bundling the data and methods that operate on the data into a single unit, and restricting access to some of the object’s components.

      In this analogy:

      • Your checking account balance is the data.
      • The ATM is the interface that provides controlled access to the data.
      • The process of checking your balance is the method that allows you to access the data.

      When you use the ATM to check your balance, you interact with a secure interface that retrieves and displays the information without exposing the underlying details of the bank’s database or the internal workings of the ATM. This encapsulation ensures that your sensitive financial information is protected and only accessible through authorized methods.