OOP Basics
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects — instances of classes. Instead of writing all logic in a single script, OOP models real-world entities as objects with properties (data) and behaviors (methods).
Java is fundamentally an OOP language. Everything in Java lives inside a class. Understanding classes, objects, constructors, and methods is essential to writing Java code effectively.
Classes and objects
- → Class — A blueprint or template that defines properties and behaviors.
- → Object — An instance of a class, created with the
newkeyword. - → Instance variables — Variables that belong to an object (its state).
- → Methods — Functions that belong to a class (its behavior).
Building your first class
Let's create a simple Person class with a constructor, instance variables, and a method. The constructor initializes the object when it's created, and methods define what the object can do.
Try it
Create a third Person object in the main method. Call its greet() method and see the output update.
Constructors
A constructor is a special method that runs when you create an object with new. It has the same name as the class and no return type. You can have multiple constructors to provide different ways of creating objects.
class Car {
String make;
String model;
int year;
// Full constructor
Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
// Default constructor
Car() {
this.make = "Unknown";
this.model = "Unknown";
this.year = 2024;
}
String getInfo() {
return year + " " + make + " " + model;
}
}
// Usage:
Car car1 = new Car("Toyota", "Camry", 2023);
Car car2 = new Car(); // Uses default constructor
Instance variables vs local variables
Instance variables are declared inside a class but outside any method. They belong to each object and retain their values between method calls. Local variables are declared inside a method and exist only while that method runs — they're destroyed when the method returns.
class Dog {
String name; // Instance variable — persists
int trickCount = 0; // Instance variable — persists
void learnTrick() {
int points = 10; // Local variable — destroyed after method
trickCount += points;
System.out.println(name + " learned a trick! Score: " + trickCount);
}
}
Dog rex = new Dog();
rex.name = "Rex";
rex.learnTrick(); // Rex learned a trick! Score: 10
rex.learnTrick(); // Rex learned a trick! Score: 20 (count persists)
The this keyword
The this keyword refers to the current object — the instance on which a method is called. It's especially useful in constructors and methods where a parameter has the same name as an instance variable.
class Point {
int x;
int y;
// 'this' disambiguates parameter from instance variable
Point(int x, int y) {
this.x = x; // this.x is the instance variable
this.y = y; // this.y is the instance variable
// x and y without 'this' refer to the parameters
}
double distanceTo(Point other) {
int dx = this.x - other.x;
int dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
Quiz
Create a Car class with fields for make, model, and year. Add a constructor and a method called getInfo() that returns a description like "2023 Toyota Camry". Create two car objects and print their info.