Module 6: Object-Oriented Programming Concepts (Classes, Objects, and Methods)
6.1 Introduction
Java is fundamentally an Object-Oriented Programming (OOP) language. OOP organizes code into classes and objects that represent real-world entities. This helps make code more reusable, modular, and maintainable.
6.2 Key OOP Concepts
- Class – A blueprint or template for creating objects.
- Object – An instance of a class; it has state (variables) and behavior (methods).
- Method – A block of code that performs an action.
- Constructor – A special method used to initialize objects.
6.3 Defining a Class and Creating Objects
public class Car {
// Fields (attributes)
String brand;
int year;
double price;
// Method
void startEngine() {
System.out.println(brand + " engine started!");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Create an object
myCar.brand = "Toyota";
myCar.year = 2022;
myCar.price = 35000;
System.out.println("Brand: " + myCar.brand);
myCar.startEngine();
}
}
Output:
Brand: Toyota
Toyota engine started!
6.4 Constructors
A constructor initializes an object when it’s created. It has the same name as the class and no return type.
public class Person {
String name;
int age;
// Constructor
Person(String n, int a) {
name = n;
age = a;
}
void greet() {
System.out.println("Hello, my name is " + name + " and I'm " + age + " years old.");
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person("Liam", 30);
p1.greet();
}
}
Output:
Hello, my name is Liam and I'm 30 years old.
6.5 The this Keyword
this refers to the current object of the class. It helps differentiate between class fields and parameters when they have the same name.
public class Student {
String name;
Student(String name) {
this.name = name; // Refers to the class variable
}
}
6.6 Method Overloading
You can define multiple methods with the same name but different parameter lists. This is known as method overloading.
public class MathUtil {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
String add(String a, String b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathUtil m = new MathUtil();
System.out.println(m.add(5, 10)); // 15
System.out.println(m.add(3.5, 2.5)); // 6.0
System.out.println(m.add("Hi ", "Java"));// Hi Java
}
}
6.7 Access Modifiers
Access modifiers control visibility of class members:
public– accessible everywhereprivate– accessible only inside the same classprotected– accessible in subclasses and same package- default (no modifier) – accessible in the same package
Example:
public class BankAccount {
private double balance;
public BankAccount(double amount) {
balance = amount;
}
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
6.8 Encapsulation
Encapsulation means keeping data (fields) private and providing controlled access through public methods (getters/setters).
public class Employee {
private String name;
private double salary;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public void setSalary(double s) {
if (s > 0) {
salary = s;
}
}
public double getSalary() {
return salary;
}
}
6.9 Static Members
static variables and methods belong to the class rather than to specific objects.
public class Counter {
static int count = 0;
Counter() {
count++;
}
static void showCount() {
System.out.println("Number of objects: " + count);
}
public static void main(String[] args) {
new Counter();
new Counter();
Counter.showCount(); // Number of objects: 2
}
}
6.10 Practice Exercises
- Car Class: Create a
Carclass with fields for brand, year, and price. Add a constructor and a method to display details. - Bank Account: Implement a class with deposit and withdraw methods, using private balance and getters/setters.
- Student Info: Create a class to store name, ID, and marks. Include a method to calculate average score.
- Overloaded Methods: Write a utility class with overloaded methods to calculate area of a circle, rectangle, and triangle.
- Static Counter: Create a class that counts how many objects have been created.
6.11 Summary
- OOP organizes programs into classes and objects.
- Constructors initialize new objects.
- Use
thisto refer to current object fields. - Encapsulation secures data using private fields and getters/setters.
- Static members belong to the class, not instances.