Designing Classes and Objects
A class is a template; an object is an instance of that template with its own state.
From Template to Instance
When you design an object-oriented program, you usually begin by describing a kind of thing rather than one particular thing. A class provides that description as a template. An object is an instance created from the template, and each object has its own state. This distinction lets one class describe a common structure while multiple objects hold their own data.
Generated example: Imagine a class called LibraryBook as a template. One object could represent a mathematics book, while another object could represent a history book. Both objects come from the same class design, but each object has its own state.
Class Components
A class has two core components: attributes and methods. Attributes store data. Methods define behavior. In a class design, attributes describe the data associated with an object, while methods describe actions or behavior associated with that object.
Separating Data from Behavior
Design a simple class concept for a music player.
Choose attributes: Generated example: The music player's current track and volume can be treated as data stored by the object. These are attributes because attributes store data.
Choose methods: Generated example: Actions such as playing a track or changing the volume can be treated as behavior. These are methods because methods define behavior.
Create separate objects: Generated example: Two music-player objects can follow the same class design while maintaining their own state.
A class design groups stored data as attributes and behavior as methods. Each object created from that design can have its own state.
The Object Lifecycle
An object has a lifecycle that begins when it is created and initialized, continues while it is used, and ends when it is destroyed. The constructor belongs at the initialization stage: constructors initialize objects. The destructor belongs before destruction: destructors clean up before an object is destroyed. Destructors are rarely needed.
Following One Object
Trace the lifecycle of a generated object representing a classroom device.
Create: A classroom device object is created from its class template.
Initialize: Its constructor initializes the object, establishing its initial state.
Use: The object is used while its attributes and methods represent its state and behavior.
Clean up: Before destruction, a destructor can perform cleanup. Destructors are rarely needed.
The lifecycle moves from creation to initialization, then use, and finally cleanup before destruction.
Extending a Class
Inheritance lets a child class extend a parent class. The child can reuse code from the parent and specialize that design by extending or changing behavior. This makes inheritance a mechanism for reusing and specializing class code rather than designing every related class independently.
Reusing a General Design
Design a parent class and two child classes for a generated set of transport objects.
Define the parent: Use a Transport class as a general parent design. It represents the shared class concept for related transport objects.
Create a child: Create a Bicycle child class that reuses the parent design and adds or specializes behavior for bicycles.
Create another child: Create a Bus child class that also reuses the parent design while adding or specializing behavior for buses.
Inheritance allows related child classes to reuse a parent class and specialize the resulting designs.
Design Decisions and Mistakes
Treating a class and an object as the same thing.
A class is a template, while an object is an instance of that template with its own state.
Fix:
Use the class name for the general design and object names for particular instances.Confusing attributes with methods.
Attributes store data, while methods define behavior.
Fix:
Ask whether the class is storing information or defining an action. Stored information belongs with attributes; behavior belongs with methods.Using a constructor as if it were a destructor.
Constructors initialize objects, while destructors clean up before destruction.
Fix:
Place initialization at object creation and cleanup before object destruction.Assuming every class needs a destructor.
Destructors are rarely needed.
Fix:
Treat a destructor as an occasional cleanup mechanism rather than a required component of every class.Describing inheritance as unrelated duplication.
Inheritance lets child classes extend parent classes and reuse and specialize code.
Fix:
Identify the shared parent design first, then describe what each child reuses and what it specializes.
| Concept | Role | Key question |
|---|---|---|
| Class | Template | What general design should related objects follow? |
| Object | Instance with its own state | Which particular instance is being represented? |
| Attribute | Stored data | What data belongs to the object? |
| Method | Defined behavior | What behavior belongs to the class? |
| Constructor | Object initialization | How is the object initialized? |
| Destructor | Cleanup before destruction | What cleanup occurs before destruction? |
| Inheritance | Class extension and code reuse | What can a child class reuse or specialize? |
Practice the Design
Generated practice: Design a class concept for a school course. Identify two attributes, two methods, one constructor responsibility, and one possible child class that could inherit from the course class.
Hints
- Begin by identifying data that a course object would store.
- Choose behaviors that describe what a course object can do.
- Describe initialization as the constructor's responsibility.
- For inheritance, choose a more specialized kind of course and explain what it could reuse or specialize.
What do you think happens?
A class template produces two objects. Must those two objects have identical state?
Reveal answer
Answer: No, because each object has its own state.
A class is a template, but an object is an instance of that template with its own state.
Design Principles to Remember
- A class is a template; an object is an instance of that template with its own state.
- Attributes store data, and methods define behavior.
- A constructor initializes an object when it is created.
- A destructor performs cleanup before destruction and is rarely needed.
- Inheritance lets child classes extend parent classes by reusing and specializing code.
Key Takeaways
- A class describes a general design, while an object is a particular instance with its own state.
- Attributes represent stored data and methods represent defined behavior.
- Constructors initialize objects, and destructors clean up before destruction but are rarely needed.
- Inheritance allows child classes to reuse, extend, and specialize parent-class code.