🧱 Object-Oriented Programming

Classes & Objects

⏱ 2 hr3 topicsInteractive
🎯 By the end: You can explain what objects and classes are, how objects bundle state and behaviour, how objects communicate by method calls, and why a class is a user-defined data type.

The two most important words in Java are class and object. Get these clear and everything else follows (ICSE Class 9).

1Objects: state and behaviour

An object represents a real-world thing in a program. It bundles two things:

  • State — the data it holds (its attributes), stored in member variables.
  • Behaviour — what it can do, written as member methods.

For a Student object: state = name, marks; behaviour = display(), calculateGrade(). Bundling data with its methods is encapsulation.

Key points
  • An object models a real thing and bundles state (data) + behaviour (actions).
  • State lives in member variables; behaviour is written as member methods.
  • Bundling data with its methods is encapsulation.

2Classes: blueprint, factory & message passing

A class is the design that describes what its objects have and can do. It is both a blueprint and an object factory:

class Student {
  String name;            // state
  int marks;

  void display() {        // behaviour
    System.out.println(name + " : " + marks);
  }
}

Student a = new Student();   // one object (the factory makes it)
Student b = new Student();   // another object

Message passing = objects communicate by calling each other's methods. Writing a.display() sends the message "display yourself" to object a.

Key points
  • A class is a blueprint that describes its objects, and a factory that creates them with new.
  • Each object has its own copy of the member variables.
  • Objects communicate by message passing — calling each other's methods, e.g. a.display().

3A class is a user-defined data type

Java has built-in (primitive) types like int and char. When you write a class, you create a new data type of your own:

int x;          // x is of the built-in type int
Student s;      // s is of the user-defined type Student

Because a class groups several values (and methods) together, it is called a composite (made of parts) data type — unlike a primitive, which holds a single simple value.

So a class does two jobs at once: it's a blueprint for objects AND a new (composite) data type you can declare variables of.
Key points
  • Defining a class creates a new, user-defined data type (e.g. Student).
  • It is a composite type — it groups several values and methods together.
  • A primitive type holds one simple value; a class type holds an object.

★ Practical: build a class

In BlueJ, write and use a class:

  1. Write a class Book with member variables title (String) and price (double).
  2. Add a member method display() that prints both.
  3. Create two Book objects and call display() on each (message passing).
  4. State which variables are primitive types and which are user-defined.

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.