Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo
Introduction to Python Programming

11.1 Object-oriented programming basics

Introduction to Python Programming11.1 Object-oriented programming basics

Learning objectives

By the end of this section you should be able to

  • Describe the paradigm of object-oriented programming (OOP).
  • Describe the concepts of encapsulation and abstraction as they relate to OOP, and identify the value of each concept.

Grouping into objects

Object-oriented programming (OOP) is a style of programming that groups related fields, or data members, and procedures into objects. Real-world entities are modeled as individual objects that interact with each other. Ex: A social media account can follow other accounts, and accounts can send messages to each other. An account can be modeled as an object in a program.

Checkpoint

Using objects to model social media

Concepts in Practice

OOP

1.
Consider the example above. Which is a field in Ellis's 12/18/23 post?
  1. Ellis's followers list
  2. Ellis's username
2.
What does an object typically model in object-oriented programming?
  1. program code
  2. real-world entity

Encapsulation

Encapsulation is a key concept in OOP that involves wrapping data and procedures that operate on that data into a single unit. Access to the unit's data is restricted to prevent other units from directly modifying the data. Ex: A ticket website manages all transactions for a concert, keeping track of tickets sold and tickets still available to avoid accidental overbooking.

Checkpoint

Encapsulating concert ticket information

Concepts in Practice

Encapsulation

3.
Consider the example above. Suppose the venue has to be changed due to inclement weather. How should a programmer change the website object to allow changes to the Venue field?
  1. Add a procedure to change the field.
  2. Allow users direct access to the field.
  3. No valid way to allow changes to the field.
4.
Which is a benefit of encapsulation when developing complex programs?
  1. All objects can easily access and modify each other's data.
  2. Each object's data is restricted for intentional access.

Abstraction

Abstraction is a key concept in OOP in which a unit's inner workings are hidden from users and other units that don't need to know the inner workings. Ex: A driver doesn't usually need to know their car engine's exact, numerical temperature. So the car has a gauge to display whether the engine temperature is within an appropriate range.

Checkpoint

Abstracting data in a countdown calculator

Concepts in Practice

Abstraction

5.
Consider the car example in the paragraph above. Suppose the designer decided to remove the engine temperature indicator. Would this be a good use of abstraction?
  1. yes
  2. no
6.
Which is a benefit of abstraction?
  1. improved view of information
  2. high visibility of information
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Feb 26, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.