Introduction
In Week 4, we learned arrays and strings, which are essential for storing and manipulating data.
Now, in Week 5, we enter the heart of Java programming: Object-Oriented Programming (OOP).
OOP allows you to model real-world entities in code, making programs more organized, reusable, and scalable.
By the end of this article, you’ll understand:
- What classes and objects are
- How to use constructors
- The
thiskeyword - Real-world OOP examples
What Is Object-Oriented Programming (OOP)?
OOP is a programming paradigm based on objects.
Objects are instances of classes, which act as blueprints.
Key Concepts in OOP:
- Class – Blueprint or template
- Object – Instance of a class
- Encapsulation – Protecting data with access modifiers
- Inheritance – Reusing code from parent classes
- Polymorphism – Ability to take many forms
- Abstraction – Hiding unnecessary details
Today, we’ll focus on classes, objects, constructors, and the this keyword.
1️⃣ Classes in Java
A class defines:
- Properties (variables)
- Behaviors (methods)
Syntax:
class ClassName {
// properties
// methods
}
Example:
class Car {
String color;
String model;
void displayInfo() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
}
}
2️⃣ Objects in Java
An object is an instance of a class.
Example:
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // create object
myCar.model = "Toyota";
myCar.color = "Red";
myCar.displayInfo();
}
}
Output:
Car Model: Toyota
Car Color: Red
3️⃣ Constructors in Java
A constructor is a special method used to initialize objects.
- Name must match class name
- No return type (not even void)
- Called automatically when object is created
Example:
class Car {
String color;
String model;
// Constructor
Car(String c, String m) {
color = c;
model = m;
}
void displayInfo() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Blue", "Honda");
Car car2 = new Car("Black", "BMW");
car1.displayInfo();
car2.displayInfo();
}
}
Output:
Car Model: Honda
Car Color: Blue
Car Model: BMW
Car Color: Black
4️⃣ The this Keyword
this refers to the current object. It is commonly used to:
- Resolve naming conflicts between variables and parameters
- Call other constructors within the class
Example:
class Car {
String color;
String model;
Car(String color, String model) {
this.color = color; // resolve naming conflict
this.model = model;
}
void displayInfo() {
System.out.println("Car Model: " + this.model);
System.out.println("Car Color: " + this.color);
}
}
5️⃣ Real-World Example: Student Class
class Student {
String name;
int age;
double cgpa;
// Constructor
Student(String name, int age, double cgpa) {
this.name = name;
this.age = age;
this.cgpa = cgpa;
}
void displayStudent() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("CGPA: " + cgpa);
System.out.println("--------------");
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Rahim", 20, 3.8);
Student s2 = new Student("Karim", 21, 3.6);
s1.displayStudent();
s2.displayStudent();
}
}
Output:
Name: Rahim
Age: 20
CGPA: 3.8
--------------
Name: Karim
Age: 21
CGPA: 3.6
--------------
Common Beginner Mistakes
- Forgetting to create objects before accessing class properties
- Using constructors incorrectly (wrong name or return type)
- Not understanding
thiskeyword - Using static methods/variables when they should be instance-based
Summary
In Week 5, you learned:
- Classes as blueprints
- Objects as instances
- Constructors for initialization
- The
thiskeyword to refer to current object - Real-world examples with cars and students
Mastering OOP is essential before moving on to advanced concepts like inheritance, polymorphism, and abstraction.
Week 6 Preview 👀
Next week, we’ll explore Inheritance, Polymorphism & Abstraction:
- Reusing code with
extends - Overriding methods
- Abstract classes vs Interfaces
- Java 8 default methods
This is where Java truly shines in building scalable, professional applications.