ProgramGuru

Classes and Objects: State and Methods

Give each student their own marks

Maya and Ravi have different marks, but both need a total operation. A class can describe the common structure while each object keeps its own values.

An object is an individual value with state and behavior. A class describes how related objects are constructed. An object created from it is an instance.

  1. Create instances

    Explain new and constructor initialization.

  2. Read the receiver

    Follow this during a method call on one student.

  3. Choose structure

    Recognize when a plain record and function is enough.

One class, separate student state

  1. Class

    Describe fields and a total operation.

    StudentMarks
  2. Maya instance

    Store her individual marks.

    40 + 35 → 75
  3. Ravi instance

    Store a different pair in a separate object.

    28 + 42 → 70
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

Shared behavior does not require shared field values

constructor receives inputs and initializes fields on this. During the shown method calls, this refers to the object before the dot, such as maya in maya.total().

A method is a function associated with an object. Calling total reads that object’s fields and returns their sum. Declaring the class does not create all the student instances in advance.

Before starting, review records and object references. These examples use JavaScript; other languages can use different syntax and rules.

Calculate totals for two objects

new creates each object and runs its constructor with the supplied marks. The two calls use different receiver state and return different totals.

JavaScript class syntax builds on its prototype-based object system. This lesson follows ordinary method calls without needing to inspect prototypes directly.

JavaScript · local teaching example

class StudentMarks {
  constructor(maths, science) {
    this.maths = maths;
    this.science = science;
  }
  total() { return this.maths + this.science; }
}
const maya = new StudentMarks(40, 35);
const ravi = new StudentMarks(28, 42);
console.log(maya.total());
console.log(ravi.total());

Expected output

75
70

Changing one instance does not change another

Each constructed object has its own science field. Updating Maya leaves Ravi’s science unchanged.

If a constructor instead stored the same supplied mutable object in both instances, that nested state could still be shared. Separate instances do not automatically deep-copy inputs.

JavaScript · local teaching example

class Student {
  constructor(science) { this.science = science; }
}
const maya = new Student(35);
const ravi = new Student(42);
maya.science = 40;
console.log(maya.science);
console.log(ravi.science);

Expected output

40
42

Apply the rule to a small change

Create a third student with marks 25 and 30 using a total method, then display the total.

Worked answer

The new instance stores its own pair and returns 55. The class definition is reused rather than copied for each student.

JavaScript · local teaching example

class StudentMarks {
  constructor(maths, science) {
    this.maths = maths; this.science = science;
  }
  total() { return this.maths + this.science; }
}
console.log(new StudentMarks(25, 30).total());

Expected output

55

Recognize mistakes and limits

Public fields can be changed directly; a class alone does not protect a valid mark range. Encapsulation adds deliberate update boundaries.

Do not add a class for every small sum. Plain records and pure functions may be simpler. Also, extracting a method and calling it without its receiver can change this behavior in JavaScript; preserve the receiver deliberately.

For the language or tool rules used here, see JavaScript class behavior.

Check the core idea

What does maya.total() read in this example?
Choose one answer. You can try again.

Answer and explanation

The maths and science fields on maya.

Yes. this refers to maya during that call.

Apply the idea to a new situation

Does a class automatically validate its public fields?
Choose one answer. You can try again.

Answer and explanation

No; the program must define and enforce those rules.

Yes. Validation belongs to an explicit contract.

Separate the class description from each instance

A class defines construction and behavior; objects hold individual state. Trace new, constructor and method receivers, and choose classes only when the organization helps.

Connect this idea with controlled valid state and instance and static members.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Encapsulation: Keep State Valid →