🧱 Object-Oriented Programming

Objects & Classes

⏱ 3 hr3 topicsInteractive
🎯 By the end: You can explain what an object's state and behaviour are, write a simple class with member variables and methods, create objects from it, and tell primitive types from user-defined types.

The whole of object-oriented programming rests on two ideas: the object and the class. This chapter looks at them closely — what an object bundles together, and why a class is both a blueprint and a factory.

1Objects: state and behaviour

An object bundles two things together:

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

Think of a car object: its state is colour, speed and fuel; its behaviour is start, accelerate and brake. Wrapping data and the methods that act on it inside one object is called encapsulation.

Object partIn JavaCar example
StateMember variablescolour, speed
BehaviourMember methodsaccelerate(), brake()
Key points
  • An object bundles state (data) and behaviour (actions) together.
  • State is stored in member variables; behaviour is written as member methods.
  • Wrapping data with the methods that act on it is called encapsulation.

2Classes: blueprint and object factory

A class defines the member variables and methods that its objects will have. It is:

  • An abstraction for a set of similar objects (all students share the same structure).
  • An object factory — you create many objects from one class with new.
class Student {
  String name;             // state
  int marks;

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

Student a = new Student();   // one object
Student b = new Student();   // another object

Here a and b are two separate objects, each with its own name and marks, but both built from the same Student class.

Key points
  • A class is an abstraction (a blueprint) for a set of similar objects.
  • A class is an object factory: new makes many objects from one class.
  • Each object has its OWN copy of the member variables.

3Primitive vs user-defined data types

Java has two families of data types:

Primitive typesUser-defined (composite) types
Examplesint, double, char, booleanClasses you (or Java) define: Student, String, Scanner
HoldsA single simple valueAn object — many values + methods together
Created withJust assignment: int x = 5;new: Student s = new Student();
A class creates a new data type. Once you write class Student, Student can be used as a type, just like int — that's why classes are called user-defined data types.
Key points
  • Primitive types (int, double, char, boolean) hold one simple value.
  • User-defined/composite types are classes (Student, String) — objects bundling data + methods.
  • Defining a class creates a new data type you can then use like a built-in one.

★ Practical: model a real thing

In BlueJ, design 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 the title and price.
  3. Create two Book objects and call display() on each.
  4. List which of your 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.