By the end of this chapter, you will be able to:
Mastering these skills will help you build strong, efficient, and maintainable software—key abilities for success in the programming trade!
Compiled by: Ann Chebet
Object-oriented programming (OOP) is a fundamental paradigm that enables software developers to structure code in a way that models real-world entities through objects and classes. In Kenya’s dynamic software development industry, OOP facilitates the creation of modular, reusable, and maintainable code, which is essential for the development of scalable applications in sectors such as banking, healthcare, and e-commerce. This chapter focuses on demonstrating essential OOP skills with particular emphasis on namespaces, a vital feature for organizing code and preventing naming conflicts especially in large projects or collaborative environments. Mastery of namespaces allows Kenyan software professionals to build robust applications that are easier to maintain and extend.
Object-oriented programming builds software from objects that model the things a program deals with. Each object is created from a class, which describes what every object of that kind knows and can do. This topic follows one running situation: a program that keeps track of the stock of a hardware shop. It starts with the two core ideas, moves to the parts of a class, builds and runs a small program, and ends with a team exercise in which each member writes a different class from one shared design. The code is written in Java, the language used throughout this unit. The ideas are the same in C#, C++ and Python, and only the syntax changes.
An object is a single, identifiable item inside a running program. It has state, which is the data it holds, and behaviour, which is what it can do. A class is the description from which objects are built: it lists the fields that hold the state and the methods that carry out the behaviour. Creating an object from a class is called instantiation, and the object is called an instance of the class.
Example: in the stock program, StockItem is a class. One object is the hinge sets on the shelf, with the name "Hinge set", a quantity of 12 and a unit price of Ksh 450. Another object is the 4-litre tins of paint, with its own name, quantity and price. Both objects follow the same class, but each keeps its own values.
Class and object compared:
| Aspect | Class | Object |
|---|---|---|
| What it is | A blueprint written once in the source code | One item built from the blueprint while the program runs |
| Holds | The declaration of fields and methods | The actual values of the fields |
| How many | One class for each kind of thing | As many objects as the program needs |
| Example | StockItem | The hinge sets, the tins of paint |
The main parts of a class:
Three properties make objects useful. Each object has an identity, so two objects with equal values are still two objects. The state of one object is separate from the state of another, so selling hinge sets does not change the paint. The behaviour is stored with the data, so code that needs the stock value asks the object for it instead of repeating the calculation.
To implement a class, the developer turns a description of a thing into code: a class declaration, its fields, a constructor and its methods. The class is then compiled and used by other code. In Java, each public class is saved in a file with the same name as the class, so the class StockItem is saved in StockItem.java.
How the parts of a class are written in Java:
| Part | Purpose | Written in Java as |
|---|---|---|
| Class declaration | Names the class | public class StockItem { ... } |
| Field | Holds one piece of state | private int quantity; |
| Constructor | Sets the starting state | public StockItem(String name, int quantity, double unitPrice) { ... } |
| Method | Gives behaviour | public double stockValue() { ... } |
| this reference | Refers to the current object | this.quantity = quantity; |
Steps to implement a class:
Two rules are worth knowing early. If a class declares no constructor, Java supplies a constructor with no parameters that leaves the fields at their default values: 0 for numbers and null for text. As soon as the class declares any constructor, that supplied constructor is no longer provided. Members marked static belong to the class itself and are shared by every object, while members without static belong to each object separately.
A good proof that a learner understands classes is a short program that defines a class, creates objects from it and shows that each object keeps its own state. The program below uses two files. StockItem.java holds the class and ShopDemo.java holds the main method that creates the objects.
StockItem.java:
public class StockItem {
private String name;
private int quantity;
private double unitPrice;
public StockItem(String name, int quantity, double unitPrice) {
this.name = name;
this.quantity = quantity;
this.unitPrice = unitPrice;
}
public double stockValue() {
return quantity * unitPrice;
}
public void sell(int units) {
if (units > 0 && units <= quantity) {
quantity = quantity - units;
} else {
System.out.println("Cannot sell " + units + " of " + name);
}
}
public String describe() {
return String.format("%s: %d in stock at Ksh %.2f each", name, quantity, unitPrice);
}
}
ShopDemo.java:
public class ShopDemo {
public static void main(String[] args) {
StockItem hinges = new StockItem("Hinge set", 12, 450.0);
StockItem paint = new StockItem("Paint 4 litre", 5, 2100.0);
System.out.println(hinges.describe());
System.out.println(paint.describe());
hinges.sell(3);
paint.sell(8);
System.out.println(hinges.describe());
System.out.println(paint.describe());
System.out.printf("Value of hinges: Ksh %.2f%n", hinges.stockValue());
}
}
Compile both files with javac StockItem.java ShopDemo.java and run the program with java ShopDemo. The output is:
Hinge set: 12 in stock at Ksh 450.00 each
Paint 4 litre: 5 in stock at Ksh 2100.00 each
Cannot sell 8 of Paint 4 litre
Hinge set: 9 in stock at Ksh 450.00 each
Paint 4 litre: 5 in stock at Ksh 2100.00 each
Value of hinges: Ksh 4050.00
Reading the program step by step:
Common mistakes when creating objects:
Practice task: add a method restock(int units) that adds to the quantity only when units is above zero, create a third object for a box of nails, call restock on it and print its state.
Software for real clients is written by teams. One way to share the work is to agree a design first, give each member one class to build, and then join the classes together. Each member must follow the agreed design exactly so that the classes fit when they are combined.
A shared design states which classes the system needs, what each one stores and which public methods it offers, with the exact method names, parameters and return types. It may be drawn as a UML class diagram or written as a table. Every member reads the whole design, not only their own class, because their class will call the methods of the others.
Example: three trainees build a booking program for a car wash. The shared design is:
| Class | Owner | Data it keeps | Public methods |
|---|---|---|---|
| Customer | Member A | name, phone | Customer(String name, String phone), getName(), getPhone() |
| WashBay | Member B | number, whether it is free | WashBay(int number), getNumber(), isFree(), occupy(), release() |
| Booking | Member C | customer, bay, service, price | Booking(Customer customer, WashBay bay, String service, double price), summary() |
Each member owns one class and writes, compiles and tests it alone, without editing a class owned by someone else. This allows parallel progress without two people changing the same file. A change to the design, such as a new method, is discussed with the whole team and written into the design first.
Classes work together through their public methods, so the names, parameter types and return types agreed in the design must be followed exactly. Member C writes the Booking class knowing only that WashBay has an occupy method and a getNumber method. If Member B renamed occupy to take, Booking would fail to compile when the classes are joined.
The three classes written by the three members:
public class Customer {
private final String name;
private final String phone;
public Customer(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
}
public class WashBay {
private final int number;
private boolean free = true;
public WashBay(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public boolean isFree() {
return free;
}
public void occupy() {
free = false;
}
public void release() {
free = true;
}
}
public class Booking {
private final Customer customer;
private final WashBay bay;
private final String service;
private final double price;
public Booking(Customer customer, WashBay bay, String service, double price) {
this.customer = customer;
this.bay = bay;
this.service = service;
this.price = price;
bay.occupy();
}
public String summary() {
return String.format("%s booked %s in bay %d for Ksh %.2f",
customer.getName(), service, bay.getNumber(), price);
}
}
A tool such as Git keeps every version of the project in one shared repository. Each member works on a separate branch for their class and merges it into the main branch when it compiles and passes its own tests. Git records who changed what, lets the team compare versions, and helps them resolve a conflict if two people edited the same lines.
After the classes are written, the team joins them and tests them together. An integration test creates objects of every class and checks that they cooperate. The test below books a bay for a customer and checks that the bay becomes taken:
public class Main {
public static void main(String[] args) {
Customer customer = new Customer("Wanjiru", "0700 000 001");
WashBay bay = new WashBay(1);
if (bay.isFree()) {
Booking booking = new Booking(customer, bay, "full wash", 600.0);
System.out.println(booking.summary());
}
if (!bay.isFree()) {
System.out.println("Bay " + bay.getNumber() + " is now taken");
}
}
}
Output:
Wanjiru booked full wash in bay 1 for Ksh 600.00
Bay 1 is now taken
The price and the customer details are values chosen for the exercise. When the integration test fails, the team reads the error to find which class and which method do not match the design, and the owner of that class repairs it. Teams that use continuous integration let a server compile and test the whole project after every merge, so an interface mismatch is found within minutes.
Short regular meetings help the team share progress and ask questions before small doubts become integration faults. Each class carries comments that say what its methods do, and the design document is updated whenever the team agrees a change. Clear documentation lets a member who joins late, or who must repair a class written by someone else, understand it without guessing.
Create a free account to open more of this chapter.
Free: practical guides, quick cards, workplace scenarios and more.
Create a free accountThis chapter provided a comprehensive demonstration of object-oriented programming skills, beginning with defining class methods that meet application requirements and emphasizing best practices for method naming and functionality. It explored the development of classes with multiple methods and their practical use in small applications, followed by conducting code reviews to ensure method declarations align with the intended application needs. The role of namespaces was examined to organize code effectively, including implementation techniques and the benefits of using namespaces in collaborative environments. Data abstraction concepts were introduced, highlighting their importance and practical implementation through abstract classes and pair programming exercises. The chapter also covered object encapsulation, detailing access modifiers such as private, public, and protected, and demonstrated encapsulation advantages through class design and presentations. Further, the use of class templates was discussed, focusing on creating generic templates and applying them to various data types in collaborative projects. Finally, the chapter addressed class inheritance by explaining different types and applying them in program hierarchies, and concluded with polymorphism, detailing its compile-time and runtime forms, practical implementation, and the benefits of polymorphism in software development.
Type: Individual
| Tools & Equipment | Materials |
|---|---|
| Computer installed with Windows 7 OS or higher | Java source code files |
| Java Development Kit (JDK) | Flash disk |
| NetBeans or Eclipse IDE |
| S/N | Item | Quantity |
|---|---|---|
| 1 | Computer installed with Windows 7 OS or higher | 1 Pc per Candidate |
| 2 | Java Development Kit (JDK) Installation Media | 1 Pc per Candidate |
| 3 | NetBeans or Eclipse IDE | 1 Pc per Candidate |
| 4 | Flash disk | 1 Pc per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Define Class and Attributes | |||
| Created a class named Student (Award 1 mark for correct class declaration or zero) | 1 | ||
| Declared attributes: name (String), age (int), grade (String) (Award 1 mark for each correctly declared attribute or zero) | 2 | ||
| Sub-Total | 3 | ||
| TASK 2: Implement Class Methods | |||
| Implemented displayDetails() method to print all attribute values (Award 2 marks for correct method implementation or zero) | 2 | ||
| Implemented updateGrade(String newGrade) method to update grade attribute (Award 2 marks for correct method implementation or zero) | 2 | ||
| Implemented calculateYearOfBirth(int currentYear) method returning year of birth (Award 3 marks for correct calculation and return statement or zero) | 3 | ||
| Sub-Total | 7 | ||
| TASK 3: Testing and Demonstration | |||
| Instantiated a Student object with sample values (Award 1 mark for correct object instantiation or zero) | 1 | ||
| Called displayDetails() method and output is correct (Award 2 marks for correct output or zero) | 2 | ||
| Updated grade using updateGrade() and verified change (Award 2 marks for correct update or zero) | 2 | ||
| Called calculateYearOfBirth() with current year and verified output (Award 2 marks for correct output or zero) | 2 | ||
| Sub-Total | 7 | ||
| TASK 4: Code Quality and Documentation | |||
| Used appropriate method and variable naming conventions (Award 2 marks for consistent and meaningful names or zero) | 2 | ||
| Included inline comments explaining methods and logic (Award 2 marks for relevant comments or zero) | 2 | ||
| Sub-Total | 4 | ||
| PRODUCT CHECKLIST | |||
| Class Student compiles without errors and methods function as specified (Award 5 marks for fully functional, error-free class or zero) | 5 | ||
| Sub-Total | 5 | ||
| GRAND TOTAL | 26 | ||
Type: Individual
| Tools & Equipment | Materials |
|---|---|
| Computer with Windows 7 OS or higher | Java source code files |
| Java Development Kit (JDK) | Documentation files |
| NetBeans or Eclipse IDE | |
| Flash disk or CD RW |
| S/N | Item | Quantity |
|---|---|---|
| 1 | Computer with Windows 7 OS or higher | 1 Pc per Candidate |
| 2 | Java Development Kit (JDK) Installation Media | 1 Pc per Candidate |
| 3 | NetBeans or Eclipse IDE | 1 Pc per Candidate |
| 4 | Flash disk or CD RW | 1 Pc per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Create Java Class and Methods | |||
| Implemented class 'Product' with attributes: productName (String), price (double), quantity (int) (Award 1 mark for each correctly declared attribute or zero) | 3 | ||
| Included methods: displayDetails(), updatePrice(double newPrice), updateQuantity(int newQuantity) (Award 1 mark for each correctly implemented method or zero) | 4 | ||
| Used appropriate access modifiers and included inline comments for methods (Award 2 marks for proper use of access modifiers and meaningful comments) | 2 | ||
| Sub-Total | 9 | ||
| TASK 2: Instantiate Objects and Demonstrate Methods | |||
| Instantiated three 'Product' objects with distinct attribute values (Award 1 mark per correctly instantiated object or zero) | 3 | ||
| Called displayDetails() for each object and printed output to console (Award 2 marks for correct method calls and output display) | 2 | ||
| Demonstrated use of updatePrice() and updateQuantity() on at least one object and displayed updated details (Award 4 marks for correct method usage and output) | 4 | ||
| Sub-Total | 9 | ||
| TASK 3: Debugging and Testing | |||
| Detected and fixed at least one logical error in the program (Award 2 marks for identifying and correcting a logic error) | 2 | ||
| Detected and fixed at least one compilation error (e.g., missing semicolon or type mismatch) (Award 2 marks for identifying and correcting a compilation error) | 2 | ||
| Used debugging tools or print statements to validate program workflow (Award 2 marks for applying debugging techniques) | 2 | ||
| Tested edge cases such as zero quantity or negative price and handled appropriately (Award 2 marks for testing and handling edge cases) | 2 | ||
| Sub-Total | 8 | ||
| TASK 4: Package and Deploy Application | |||
| Prepared project folders and assets including source code and documentation (Award 1 mark for organized project folder) | 1 | ||
| Generated runnable Java archive (.jar) file of the application (Award 1 mark for generating runnable .jar) | 1 | ||
| Successfully executed the packaged application without errors (Award 1 mark for successful execution) | 1 | ||
| Sub-Total | 3 | ||
| TASK 5: Write Deployment Guide and Use Case | |||
| Wrote a step-by-step installation and deployment guide including screenshots (Award 2 marks for clear and complete deployment guide with screenshots) | 2 | ||
| Explained required dependencies such as JDK and environment variables (Award 2 marks for explaining dependencies) | 2 | ||
| Proposed a real-world use case where the class could be adopted in a business (Award 1 mark for relevant real-world use case) | 1 | ||
| Sub-Total | 5 | ||
| TASK 6: Documentation Submission | |||
| Submitted complete, well-formatted source code with inline comments (Award 3 marks for complete and well-commented source code) | 3 | ||
| Included UML class diagram and screenshots of application output (Award 4 marks for including UML and screenshots) | 4 | ||
| Provided a technical summary explaining system workflow from user perspective (Award 5 marks for clear technical summary) | 5 | ||
| Submitted a well-organized digital folder containing all files and documentation (Award 6 marks for professional folder structure and presentation) | 6 | ||
| Sub-Total | 18 | ||
| PRODUCT CHECKLIST | |||
| Class 'Product' correctly implemented with required attributes and methods (Award 5 marks for correct class implementation matching specifications) | 5 | ||
| Objects instantiated with accurate attribute values and methods demonstrated correctly (Award 5 marks for correct object creation and method demonstration) | 5 | ||
| Application runs without compilation or runtime errors and handles edge cases gracefully (Award 5 marks for error-free execution and edge case handling) | 5 | ||
| Packaged application (.jar) runs successfully on target system (Award 3 marks for successful packaged application execution) | 3 | ||
| Sub-Total | 18 | ||
| GRAND TOTAL | 70 | ||
At the start of this chapter we promised you would be able to:
Tick each one you can genuinely do.
So, are you there yet?
You're competent when you can confidently do 50% or more of what this chapter promised.
Sign in to record how you're doing.