Classes and Objects
Object-Oriented Programming (OOP) is core to Java. This lesson covers the basics of classes and objects.
What is Object-Oriented Programming
OOP is a programming paradigm that bundles data and methods that operate on that data together.
OOP vs Procedural Programming
| Feature | Procedural | Object-Oriented |
|---|---|---|
| Thinking style | Step-by-step execution | Organize by objects |
| Code structure | Functions | Classes and objects |
| Best for | Simple tasks | Complex systems |
Defining a Class
A class is a template for objects, defining their attributes and methods.
Syntax
JAVA
public class ClassName {
// Attributes (member variables)
dataType attributeName;
// Methods (member methods)
returnType methodName(parameterList) {
// Method body
}
}
Example: Define a Student Class
JAVA
public class Student {
// Attributes
String name;
int age;
double score;
// Methods
public void study() {
System.out.println(name + " is studying");
}
public void showInfo() {
System.out.println("Name: " + name + ", Age: " + age + ", Score: " + score);
}
}
Creating Objects
Use the new keyword to create objects.
Syntax
JAVA
ClassName objectName = new ClassName();
Example: Create and Use Objects
JAVA
public class StudentTest {
public static void main(String[] args) {
// Create object
Student stu1 = new Student();
// Set attributes
stu1.name = "Alice";
stu1.age = 20;
stu1.score = 95.5;
// Call methods
stu1.study(); // Alice is studying
stu1.showInfo(); // Name: Alice, Age: 20, Score: 95.5
// Create another object
Student stu2 = new Student();
stu2.name = "Bob";
stu2.age = 22;
stu2.score = 88.0;
stu2.study(); // Bob is studying
stu2.showInfo(); // Name: Bob, Age: 22, Score: 88.0
}
}
Constructors
Constructors are used to initialize attributes when creating objects.
Characteristics
| Characteristic | Description |
|---|---|
| Same name as class | Required |
| No return type | Not even void |
| Called automatically with new | No manual call needed |
| Can be overloaded | Multiple constructors |
No-Argument Constructor
JAVA
public class Student {
String name;
int age;
// No-argument constructor
public Student() {
System.out.println("Student object created");
}
}
Parameterized Constructor
JAVA
public class Student {
String name;
int age;
// Parameterized constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Example: Constructors
JAVA
public class Student {
String name;
int age;
double score;
// No-argument constructor
public Student() {
this.name = "Unknown";
this.age = 0;
this.score = 0;
}
// Parameterized constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// Full constructor
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
public void showInfo() {
System.out.println("Name: " + name + ", Age: " + age + ", Score: " + score);
}
}
public class ConstructorDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.showInfo(); // Name: Unknown, Age: 0, Score: 0
Student s2 = new Student("Alice", 20);
s2.showInfo(); // Name: Alice, Age: 20, Score: 0.0
Student s3 = new Student("Bob", 22, 95.5);
s3.showInfo(); // Name: Bob, Age: 22, Score: 95.5
}
}
⚠️ Note: If no constructor is defined, Java automatically provides a no-argument constructor. If you define a parameterized constructor, the no-argument constructor is not automatically provided.
The this Keyword
this is a reference to the current object.
Use 1: Distinguish Member Variables from Local Variables
JAVA
public class Student {
String name;
public void setName(String name) {
this.name = name; // this.name is the member variable, name is the parameter
}
}
Use 2: Call Other Constructors
JAVA
public class Student {
String name;
int age;
public Student() {
this("Unknown", 0); // Call parameterized constructor
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Example: Using this
JAVA
public class Point {
int x, y;
public Point() {
this(0, 0);
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void move(int x, int y) {
this.x += x;
this.y += y;
}
public void show() {
System.out.println("Point: (" + x + ", " + y + ")");
}
public static void main(String[] args) {
Point p = new Point(3, 5);
p.show(); // Point: (3, 5)
p.move(2, -1);
p.show(); // Point: (5, 4)
}
}
Object Memory Model
Memory Allocation
JAVA
Student stu = new Student("Alice", 20);
stu(reference variable) is stored in stack memorynew Student()(object) is stored in heap memorystuholds the address of the object
Example: Multiple References to Same Object
JAVA
public class MemoryDemo {
public static void main(String[] args) {
Student s1 = new Student("Alice", 20);
Student s2 = s1; // s2 and s1 point to the same object
System.out.println(s1.name); // Alice
System.out.println(s2.name); // Alice
s2.name = "Bob";
System.out.println(s1.name); // Bob (s1 is also modified)
System.out.println(s2.name); // Bob
}
}
Multiple Objects
JAVA
public class MultiObject {
public static void main(String[] args) {
// Create multiple objects
Student[] students = new Student[3];
students[0] = new Student("Alice", 20);
students[1] = new Student("Bob", 22);
students[2] = new Student("Charlie", 21);
// Traverse
for (Student s : students) {
s.showInfo();
}
}
}
Objects as Method Parameters
JAVA
public class StudentUtil {
// Object as parameter
public static void printStudent(Student stu) {
System.out.println(stu.name + " - " + stu.age);
}
// Return object
public static Student createStudent(String name, int age) {
return new Student(name, age);
}
public static void main(String[] args) {
Student alice = new Student("Alice", 20);
printStudent(alice); // Alice - 20
Student bob = createStudent("Bob", 22);
bob.showInfo();
}
}
❓ Frequently Asked Questions
Q What's the difference between a class and an object?
A A class is a template. An object is an instance. The class defines the structure of attributes and methods. An object is a concrete instance.
Q Can a constructor have a return value?
A No. Constructors have no return type—not even void.
Q Can this be omitted?
A Yes, when member variables and local variables don't share the same name. But adding this makes the code clearer.
📖 Summary
- Classes are templates for objects, defining attributes and methods
- Objects are created using the new keyword
- Constructors initialize objects and can be overloaded
- this refers to the current object, used to distinguish variables and call constructors
- Objects are stored in heap memory, references in stack memory
📝 Exercises
- Rectangle class: Define a rectangle class with length and width attributes, and methods to calculate area and perimeter
- BankAccount class: Define a bank account class with deposit, withdraw, and check balance methods
- Object array: Create 5 student objects and find the one with the highest score
Next Lesson
In the next lesson, we'll learn about Encapsulation — how to protect object data.



