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.
- 1Choose 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.
- 2Choose 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.
- 3Create 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.
- 1Create
A classroom device object is created from its class template.
- 2Initialize
Its constructor initializes the object, establishing its initial state.
- 3Use
The object is used while its attributes and methods represent its state and behavior.
- 4Clean 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.
- 1Define the parent
Use a Transport class as a general parent design. It represents the shared class concept for related transport objects.
- 2Create a child
Create a Bicycle child class that reuses the parent design and adds or specializes behavior for bicycles.
- 3Create 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
Common mistake Treating a class and an object as the same thing.
What it looks likeCalling the LibraryBook class one particular book.
Why it failsA class is a template, while an object is an instance of that template with its own state.
Do this insteadUse the class name for the general design and object names for particular instances.
Common mistake Confusing attributes with methods.
What it looks likeDescribing an action as stored data or describing stored data as behavior.
Why it failsAttributes store data, while methods define behavior.
Do this insteadAsk whether the class is storing information or defining an action. Stored information belongs with attributes; behavior belongs with methods.
Common mistake Using a constructor as if it were a destructor.
What it looks likeDescribing initialization as cleanup before destruction.
Why it failsConstructors initialize objects, while destructors clean up before destruction.
Do this insteadPlace initialization at object creation and cleanup before object destruction.
Common mistake Assuming every class needs a destructor.
Why it failsDestructors are rarely needed.
Do this insteadTreat a destructor as an occasional cleanup mechanism rather than a required component of every class.
Common mistake Describing inheritance as unrelated duplication.
Why it failsInheritance lets child classes extend parent classes and reuse and specialize code.
Do this insteadIdentify 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? |
The main class-and-object concepts and their roles
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
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
- 1A class is a template; an object is an instance of that template with its own state.
- 2Attributes store data, and methods define behavior.
- 3A constructor initializes an object when it is created.
- 4A destructor performs cleanup before destruction and is rarely needed.
- 5Inheritance 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.