Concepts / Inheritance and Code Reuse

Inheritance and Code Reuse

An object bundles data (attributes) and behavior (methods) into a single, organized unit.

  • Programming

From Scattered Pieces to One Unit

Imagine tracking a car with separate variables for its color, speed, and mileage, alongside separate functions for accelerating, braking, and displaying its status. The data and actions describe the same car, but they are scattered. An object brings them together into one organized unit. It bundles data, called attributes, with behavior, called methods.

The central reuse idea in this material is to define one organized class and then create as many independent objects from it as needed. Each object follows the same structure while holding its own data values.

One Blueprint, Many Objects

A class is a blueprint or template. It specifies what data an object will hold and what actions it can perform. An object is an instance created from that blueprint. Once a class exists, many individual objects can be created from it. Those objects follow the same blueprint, but each one is independent and can contain different values for its data.

createscreatesBankAccount classblueprintAccount object 1balance: 500Account object 2balance: 2000
How does one class blueprint relate to the individual objects created from it?

Modeling Two Bank Accounts

Organize account numbers, owner names, balances, and account actions so that two accounts can be represented independently.

Choose the blueprint: Use a BankAccount class to describe the data and actions that every account object should have.

Create the first object: The first account object has its own account number, owner name, and balance. Its balance can be 500 dollars.

Create the second object: The second account object follows the same class blueprint but has its own values. Its balance can be 2000 dollars.

Reuse the actions: Both objects can support depositing money, withdrawing money, and checking the balance, while remaining separate instances.

One class organizes the common structure, while multiple objects reuse that structure with independent data values.

Data and Behavior Together

An attribute is a piece of data that belongs to an object. It stores information about the object's state or characteristics. For a Student object, possible attributes include name, student_id, and gpa. Each student object has its own values for those attributes.

A method is a function that lives inside an object. While an attribute stores data, a method performs an action. Methods typically work with the object's attributes. A Student object might use one method to calculate a letter grade from its gpa and another method to update its gpa.

containscontainscontainsStudent objectorganized unitnamestudent's namestudent_idstudent identifiergpagrade information
What data does an object contain, and where are its attributes stored?
provides data toreturnschangesgpastored attributecalculate_letter_grademethodletter grademethod resultupdate_gpamethod
How does a method use or change an object's attributes when it runs?

The important distinction is what each part contributes. Attributes describe what the object knows or stores. Methods describe what the object can do. A method may read an attribute, use it to produce a result, or change it when the object's state needs to be updated.

The Class Keyword as an Organizer

The class keyword marks the beginning of a class blueprint in Python. It is followed by a name, a colon, and an indented block. Inside that block, related attributes and methods are grouped together. This makes the class an organizational container for the data and code that belong to the same kind of object.

python

This short illustration shows the organization rather than a complete executable program. The class groups the account data and account actions in one place. In a complete implementation, those names would be defined with the appropriate Python statements and method bodies.

Mistakes About Reuse

  • Treating a class and an object as the same thing

    A class is the blueprint, while an object is an individual instance created from that blueprint.

    Fix: Use the class to describe the structure, then refer to each created account as a separate object or instance.

  • Thinking that all objects created from one class share the same data values

    Objects follow the same blueprint but can have different values for their own attributes.

    Fix: Separate the common structure from the individual values. One account can have a balance of 500 dollars while another has 2000 dollars.

  • Confusing an attribute with a method

    Attributes store information, while methods perform actions.

    Fix: Classify balance as an attribute and withdrawing money as a method or behavior.

  • Keeping related variables and functions scattered outside an organizing structure

    The related pieces become harder to understand, maintain, and reuse.

    Fix: Use a class to group the related account attributes and methods into one cohesive unit.

Practice the Model

EASY

Design a class blueprint for a Student object. List three attributes that store information and two methods that perform actions. Then explain how two student objects created from the same class could have different attribute values.

Hints
  • Use name, student_id, and gpa as possible attributes.
  • A method should describe an action, such as calculating a letter grade or updating the gpa.
  • Remember that the class provides the shared blueprint, while each object has its own values.

Checking a Student Design

Decide whether each item belongs as an attribute or a method in a Student class: name, gpa, calculate_letter_grade, update_gpa.

Classify name: Name is information about the student, so it is an attribute.

Classify gpa: Gpa stores grade information about the student, so it is an attribute.

Classify calculate_letter_grade: This performs an action using the gpa, so it is a method.

Classify update_gpa: This performs an action that changes the gpa, so it is a method.

The attributes are name and gpa. The methods are calculate_letter_grade and update_gpa.

Key Takeaways

  1. A class is a blueprint that specifies an object's data and actions.
  2. An object is an independent instance created from a class.
  3. Attributes store information about an object's state or characteristics.
  4. Methods perform actions and typically use or change the object's attributes.
  5. The class keyword groups related data and behavior, supporting code that is easier to understand, maintain, and reuse.

Key Takeaways

  • A class provides a reusable blueprint; an object is one independent instance of that blueprint.
  • Attributes hold an object's information, while methods define actions involving that information.
  • The class keyword organizes related attributes and methods into a cohesive structure.
  • Creating multiple objects from one class reuses the same organization while allowing different data values.
  • Bundling data and behavior makes programs easier to understand, maintain, debug, and reuse.