Bachelor of Science in Computer Science
Course ContentObjects
Habari! Welcome to the World of Java Objects!
Ever used M-Pesa? Think about it. When you send money, you are a specific user with your own phone number and balance. Your friend is another user with their own details. You are both "users" of the M-Pesa system, but you are separate and unique. In the world of Java, you and your friend would be considered Objects! Today, we are going to learn how to create these building blocks of programming. It's easier than catching a matatu in town during rush hour, I promise!
What Exactly is an Object?
In the real world, an object is anything you can see or touch: a car, a pen, a student, a lion. In Java, it's very similar! A Java Object is a self-contained unit that groups together two important things:
- State: These are the properties or attributes that describe the object. What does it have? For a matatu, its state could be its route number, its Sacco name, and the current number of passengers.
- Behavior: These are the actions that the object can perform. What can it do? A matatu can move, hoot, pick up passengers, and drop them off.
Real-World Scenario: Think of a student, let's call her Wanjiku.
State (Her Properties): Name is "Wanjiku", Admission Number is "C026-01-0987/2023", Course is "Computer Science".
Behavior (Her Actions): She canattendClass(),study(), andsubmitAssignment().
Omondi, another student, is a different object with his own state (his name, his admission number) but with the same set of possible behaviors.
The Blueprint: Understanding the 'Class'
Before you can build a house, you need a blueprint, right? The blueprint defines what the house will have (how many rooms, windows, doors) but it isn't the house itself. In Java, this blueprint is called a Class.
A Class is a template or blueprint from which objects are created. It defines the states (as variables) and behaviors (as methods) that all objects of that type will have.
> **Image Suggestion:** [A vibrant, digital art illustration showing an architect's blueprint for a classic Kenyan matatu on a wooden table. In the background, out of focus, are several colourful, unique matatus (one for Rongai, one for Buruburu) driving on a Nairobi street, suggesting they were all built from this one blueprint.] Let's create a blueprint for our Kenyan Matatu.
// This is the CLASS - our blueprint
public class Matatu {
// 1. State (Instance Variables)
String routeName;
int routeNumber;
int passengerCount;
int maxCapacity;
// 2. Behavior (Methods)
void boardPassenger() {
// Action to add a passenger
System.out.println("Passenger ame-board!");
}
void alightPassenger() {
// Action to remove a passenger
System.out.println("Passenger ameshuka.");
}
void displayDetails() {
// Action to show current status
System.out.println("Route: " + routeNumber + " " + routeName);
System.out.println("Passengers: " + passengerCount);
}
}
From Blueprint to Reality: Creating an Object!
Okay, we have our `Matatu` blueprint (our Class). Now, let's build the actual matatus! This process is called instantiation. We use the special keyword new to create an object from a class.
// Syntax: ClassName objectName = new ClassName();
// Let's create a matatu for the Rongai route (Route 125)
Matatu matatuYaRongai = new Matatu();
// And another one for the Buruburu route (Route 58)
Matatu matatuYaBuru = new Matatu();
We now have two distinct Matatu objects. matatuYaRongai and matatuYaBuru are two separate entities in the computer's memory, just like two real matatus on the road. Each one has its own set of states (routeName, passengerCount, etc.).
Giving Our Objects Life: Setting State and Using Behavior
Once an object is created, we can interact with it using the dot (.) operator.
1. Accessing and Changing State
We can give our matatuYaRongai its specific details.
// Setting the state for our Rongai matatu
matatuYaRongai.routeName = "Kiserian/Rongai";
matatuYaRongai.routeNumber = 125;
matatuYaRongai.passengerCount = 10;
matatuYaRongai.maxCapacity = 33;
// Setting the state for our BuruBuru matatu
matatuYaBuru.routeName = "Buruburu Phase 5";
matatuYaBuru.routeNumber = 58;
matatuYaBuru.passengerCount = 20;
matatuYaBuru.maxCapacity = 33;
2. Calling Behaviors (Methods)
Let's make our Rongai matatu do something!
// Let's see the details of the Rongai matatu
matatuYaRongai.displayDetails();
// A new passenger gets on
matatuYaRongai.boardPassenger();
Let's add a little math to our methods
We can make our methods smarter. Let's update our `Matatu` class to change the `passengerCount` automatically and check if it's full.
public class Matatu {
// ... (same variables as before)
// A smarter method
void boardPassenger() {
if (passengerCount < maxCapacity) {
passengerCount = passengerCount + 1; // Or passengerCount++;
System.out.println("Karibu ndani! A passenger has boarded. Total: " + passengerCount);
} else {
System.out.println("Gari imejaa! We can't take more passengers.");
}
}
// ... (other methods)
}
Now, when we call matatuYaRongai.boardPassenger(), it will actually perform a calculation and update its own state!
Let's Visualize an Object
Think of an object in memory as a box. The box is labeled with the object's name (e.g., matatuYaRongai) and inside, it holds its state and has buttons for its behaviors.
+-----------------------------------------+
| Object: matatuYaRongai |
| (Type: Matatu) |
+-----------------------------------------+
| |
| ------------ STATE ------------ |
| routeName: "Kiserian/Rongai" |
| routeNumber: 125 |
| passengerCount: 11 |
| maxCapacity: 33 |
| |
| ----------- BEHAVIOR ----------- |
| [ boardPassenger() ] |
| [ alightPassenger() ] |
| [ displayDetails() ] |
| |
+-----------------------------------------+
> **Image Suggestion:** [A clean, educational diagram for a PowerPoint slide. On the left, a box labeled "CLASS: Student" with properties like 'name', 'admNo', and methods 'study()', 'attendClass()'. Arrows point from this box to three distinct "OBJECT" boxes on the right. The first object box is for 'Kamau', showing his specific name and admNo. The second is for 'Wanjiku', and the third for 'Omondi', each with their unique data, visually representing that one class can create multiple, unique objects.]
Key Takeaways
- A Class is the blueprint (e.g., the plan for a matatu).
- An Object is a real instance created from that blueprint (e.g., the actual matatu KDA 123X on the Rongai route).
- Objects have State (variables) and Behavior (methods).
- We use the
newkeyword to create an object from a class. - We use the dot (
.) operator to access an object's state and behavior.
Congratulations! You've just grasped the single most important concept in Object-Oriented Programming. Everything else we learn will build on this foundation. Look around you right now. Can you identify three 'objects' in the room and list their potential states and behaviors? Keep thinking like this, and you'll be a Java pro in no time! Keep up the great work!
Pro Tip
Take your own short notes while going through the topics.