Concepts / Designing Classes and Objects

Designing Classes and Objects

A class is a template; an object is an instance of that template with its own state.

  • Programming

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.

Classshared templateObject Aown stateObject Bown statecreates an instancecreates an instance
How does one class template produce multiple objects, and how can each object have its own state?
In the real world

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.

ClassAttributesstored dataObject stateattribute valuesMethodsdefined behaviorcontainscontainsprovide data fordefine behavior involving
What belongs inside a class, and how are an object's stored attributes connected to the methods that operate on them?
Worked example

Separating Data from Behavior

Design a simple class concept for a music player.

  1. 1
    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.

  2. 2
    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.

  3. 3
    Create separate objects

    Generated example: Two music-player objects can follow the same class design while maintaining their own state.

Result

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.

CreatedInitializedconstructorUsedobject stateCleanupdestructorDestroyedinitializeready for useprepare for destructiondestroy
What happens to an object when it is created, initialized, used, and eventually destroyed?
Object creationConstructorinitializes objectInitial stateattribute valuesinvokesestablishes
How does a constructor assign an object's initial values when the object is created?
Worked example

Following One Object

Trace the lifecycle of a generated object representing a classroom device.

  1. 1
    Create

    A classroom device object is created from its class template.

  2. 2
    Initialize

    Its constructor initializes the object, establishing its initial state.

  3. 3
    Use

    The object is used while its attributes and methods represent its state and behavior.

  4. 4
    Clean up

    Before destruction, a destructor can perform cleanup. Destructors are rarely needed.

Result

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.

Parent classshared designChild class Areuses and extendsChild class Breuses and specializesextended byextended by
How does a child class reuse attributes and methods from a parent class while adding or changing behavior?
Worked example

Reusing a General Design

Design a parent class and two child classes for a generated set of transport objects.

  1. 1
    Define the parent

    Use a Transport class as a general parent design. It represents the shared class concept for related transport objects.

  2. 2
    Create a child

    Create a Bicycle child class that reuses the parent design and adds or specializes behavior for bicycles.

  3. 3
    Create another child

    Create a Bus child class that also reuses the parent design while adding or specializing behavior for buses.

Result

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 like

    Calling the LibraryBook class one particular book.

    Why it fails

    A class is a template, while an object is an instance of that template with its own state.

    Do this instead

    Use the class name for the general design and object names for particular instances.

  • Common mistake

    Confusing attributes with methods.

    What it looks like

    Describing an action as stored data or describing stored data as behavior.

    Why it fails

    Attributes store data, while methods define behavior.

    Do this instead

    Ask 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 like

    Describing initialization as cleanup before destruction.

    Why it fails

    Constructors initialize objects, while destructors clean up before destruction.

    Do this instead

    Place initialization at object creation and cleanup before object destruction.

  • Common mistake

    Assuming every class needs a destructor.

    Why it fails

    Destructors are rarely needed.

    Do this instead

    Treat a destructor as an occasional cleanup mechanism rather than a required component of every class.

  • Common mistake

    Describing inheritance as unrelated duplication.

    Why it fails

    Inheritance lets child classes extend parent classes and reuse and specialize code.

    Do this instead

    Identify the shared parent design first, then describe what each child reuses and what it specializes.

ConceptRoleKey question
ClassTemplateWhat general design should related objects follow?
ObjectInstance with its own stateWhich particular instance is being represented?
AttributeStored dataWhat data belongs to the object?
MethodDefined behaviorWhat behavior belongs to the class?
ConstructorObject initializationHow is the object initialized?
DestructorCleanup before destructionWhat cleanup occurs before destruction?
InheritanceClass extension and code reuseWhat can a child class reuse or specialize?

The main class-and-object concepts and their roles

Practice the Design

MEDIUM

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?

  • Yes, because they come from the same class

  • No, because each object has its own 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

  1. 1
    A class is a template; an object is an instance of that template with its own state.
  2. 2
    Attributes store data, and methods define behavior.
  3. 3
    A constructor initializes an object when it is created.
  4. 4
    A destructor performs cleanup before destruction and is rarely needed.
  5. 5
    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.

Related concepts

More concepts starting with D