LESSON 9
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define a class in Java.
- Create and declare Java classes.
- Explain the purpose of classes in object-oriented programming.
- Identify class attributes and methods.
- Create objects from Java classes.
3.2 Overview
Classes are one of the fundamental building blocks of Java programming. Java is a class-based and object-oriented programming language, meaning that applications are developed using classes and objects.
This lesson introduces learners to Java classes and explains how classes are used to organize program data and behaviour into reusable structures. Learners will also explore how classes contain variables and methods that define the characteristics and behaviour of objects.
Classes are widely used in:
- banking systems,
- healthcare systems,
- inventory applications,
- eCommerce platforms,
- and enterprise software systems.
Understanding Java classes is essential because all object-oriented Java applications rely on class structures.
KT0901 — Introduction to Java Classes
A class is a blueprint used to create objects in Java.
Classes define:
- object properties (attributes),
- object behaviour (methods),
- and program structure.
Structure of a Java Class
public class Student {
}
Explanation
| Component | Description |
|---|---|
| public | Access modifier |
| class | Keyword used to declare a class |
| Student | Class name |
| {} | Class body |
Importance of Classes
Classes help developers:
- organize code,
- reuse functionality,
- simplify maintenance,
- and model real-world systems.
KT0902 — Class Attributes
Attributes are variables declared inside a class.
Attributes store data related to objects created from the class.
Example
public class Student {
String name;
int age;
}
Explanation
| Attribute | Purpose |
|---|---|
| name | Stores student name |
| age | Stores student age |
Importance of Attributes
Attributes help:
- represent object state,
- store object information,
- and manage application data.
KT0903 — Class Methods
Methods define the behaviour of objects.
Methods perform actions and operations inside a class.
Example
public class Student {
void study() {
System.out.println("Student is studying");
}
}
Explanation
| Component | Description |
|---|---|
| void | No return value |
| study() | Method name |
| System.out.println() | Displays output |
Importance of Methods
Methods:
- improve code reuse,
- organize program logic,
- and simplify application functionality.
KT0904 — Creating Objects from Classes
Objects are created from classes using the new keyword.
Example
public class Main {
public static void main(String[] args) {
Student learner = new Student();
}
}
Explanation
| Component | Description |
|---|---|
| Student | Class name |
| learner | Object reference |
| new | Creates object |
| Student() | Constructor |
KT0905 — Accessing Attributes and Methods
Object attributes and methods are accessed using the dot operator.
Example
public class Main {
public static void main(String[] args) {
Student learner = new Student();
learner.name = "John";
System.out.println(learner.name);
learner.study();
}
}
Output
John
Student is studying
Importance of the Dot Operator
The dot operator allows access to:
- object variables,
- methods,
- and behaviour.
KT0906 — Multiple Objects from One Class
One class can create many objects.
Each object contains its own data.
Example
public class Main {
public static void main(String[] args) {
Student learner1 = new Student();
Student learner2 = new Student();
learner1.name = "John";
learner2.name = "Sarah";
System.out.println(learner1.name);
System.out.println(learner2.name);
}
}
Output
John
Sarah
Importance of Multiple Objects
Multiple objects allow applications to:
- manage many entities,
- store separate data,
- and process information efficiently.
KT0907 — Real-World Use of Java Classes
Java classes are used in:
- banking systems,
- school management systems,
- eCommerce platforms,
- healthcare systems,
- and mobile applications.
Examples:
- Customer class
- Product class
- Employee class
- Vehicle class
Classes help model real-world entities inside software applications.
3.5 Key Notes / Summary
- A class is a blueprint for creating objects.
- Classes contain attributes and methods.
- Attributes store object data.
- Methods define object behaviour.
- Objects are created using the new keyword.
- The dot operator accesses object members.
- Multiple objects can be created from one class.