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 part | In Java | Car example |
|---|---|---|
| State | Member variables | colour, speed |
| Behaviour | Member methods | accelerate(), brake() |
- 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 objectHere a and b are two separate objects, each with its own name and marks, but both built from the same Student class.
- 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 types | User-defined (composite) types | |
|---|---|---|
| Examples | int, double, char, boolean | Classes you (or Java) define: Student, String, Scanner |
| Holds | A single simple value | An object — many values + methods together |
| Created with | Just assignment: int x = 5; | new: Student s = new Student(); |
class Student, Student can be used as a type, just like int — that's why classes are called user-defined data types.- 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:
- Write a class Book with member variables title (String) and price (double).
- Add a member method display() that prints the title and price.
- Create two Book objects and call display() on each.
- 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.