Classes and Objects

A class is a blueprint defining state and behaviour; an object is a concrete instance with its own field values, created by a constructor, with the this/self reference letting methods act on the specific instance that received the call.

Key formulas & points

Skim these first — then read the full notes below.

  • Encapsulation bundles data with the methods that act on it
  • Access modifiers: public, private, protected
  • Static members belong to the class, not to any instance

Topic details

Introduction

This topic introduces the OOP building blocks. You define classes with fields and methods, create objects via constructors, distinguish instance members from static (class-level) members, and use access modifiers to control visibility.

Key relations & formulas

object=instanceofaclass=state(fields)+behaviourobject = instance of a class = state (fields) + behaviour
(methods)

Formulas (Indian textbook notation)

  • constructor:samenameastheclass,noreturntypeconstructor: same name as the class, no return type

Formulas (Indian textbook notation)

  • thisselfreferstothecurrentinstance\frac{this}{self} refers to the current instance

Notation and sign conventions

Relation 1 —
object=instanceofaclass=stateobject = instance of a class = state
object=instanceofaclass=state(fields)+behaviourobject = instance of a class = state (fields) + behaviour
(methods)
Write this relation with symbols exactly as in Programming in ANSI C / OOP with C++ — E. Balagurusamy before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
constructor:samenameastheclass,noreturntypeconstructor: same name as the class, no return type

Formulas (Indian textbook notation)

  • constructor:samenameastheclass,noreturntypeconstructor: same name as the class, no return type
Write this relation with symbols exactly as in Programming in ANSI C / OOP with C++ — E. Balagurusamy before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
thisselfreferstothecurrentinstance\frac{this}{self} refers to the current instance

Formulas (Indian textbook notation)

  • thisselfreferstothecurrentinstance\frac{this}{self} refers to the current instance
Write this relation with symbols exactly as in Programming in ANSI C / OOP with C++ — E. Balagurusamy before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Object orientation models a program as interacting objects, each bundling data with the operations that manipulate it — this encapsulation keeps related state and behaviour together and hides internals behind a public interface. A class describes the structure once; every object gets its own copy of instance fields but shares the class’s methods, and the implicit this reference tells a method which object’s fields to use. Static members break from this: they belong to the class as a whole, so a static counter is shared by all instances. Constructors initialise an object’s invariants at creation so it is never in an invalid state.

Assumptions and validity limits

State assumptions explicitly before using any relation for classes and objects — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In OOP viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in OOP papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to classes and objects.
4. Use equation 1:
object=instanceofaclass=stateobject = instance of a class = state
.
5. Use equation 2:
constructor:samenameastheclass,noreturntypeconstructor: same name as the class, no return type
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Classes and Objects appears in Java/C++ application development. In Indian it software curricula this topic is tested because it connects theory to classes, inheritance, and polymorphism.
GATE and semester exams often combine classes and objects with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use classes and objects?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students confuse the class (blueprint) with the object (instance), treat static members as per-object, and forget that a constructor has no return type. Accessing instance fields from a static context without an object is a frequent compilation error.

Quick revision checklist

Before attempting classes and objects problems, confirm you can:
1. Encapsulation bundles data with the methods that act on it
2. Access modifiers: public, private, protected
3. Static members belong to the class, not to any instance
Revise the solved examples in Programming in ANSI C / OOP with C++ — E. Balagurusamy and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Static versus instance members

Problem

A class has a static field count incremented in the constructor. After creating 3 objects, what is count, and how many copies exist?

Solution

count = 3, and only one copy exists — static fields are shared by the whole class, not duplicated per object, so each constructor call increments the single shared counter.

Conceptual check — Classes and Objects

Problem

In a OOP semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of classes and objects." What should a complete answer include?

📖 Standard books (India)

  • Programming in ANSI C / OOP with C++E. Balagurusamy

    Read: Syllabus unit

    Standard Indian classroom programming text