⬅ Previous Topic
Java Continue StatementNext Topic ⮕
Java Class⬅ Previous Topic
Java Continue StatementNext Topic ⮕
Java ClassObject-Oriented Programming (OOP) is not just a way of writing code—it's a philosophy. It’s how we model the real world inside our programs. Java is one of the most popular languages to embrace OOP from the ground up. If you're new to Java, understanding its object-oriented foundation is your first real step into writing clean, scalable, and maintainable software.
OOP is a programming paradigm centered around the concept of objects—instances of classes—that interact with one another. Instead of thinking in terms of functions and logic alone, OOP encourages thinking in terms of entities, their attributes, and their behaviors.
Java uses OOP to:
Let’s dive into the four foundational pillars of Java OOP:
A class is like a blueprint. An object is a real-world instance of that blueprint.
class Car {
String color;
void drive() {
System.out.println("The car is driving.");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // object creation
myCar.color = "Red";
myCar.drive(); // method call
System.out.println(myCar.color);
}
}
The car is driving.
Red
Here, Car
is the class with a property and a method. myCar
is an object created from that class. It accesses the method and variable defined in the class.
Encapsulation means bundling data and methods that work on that data into a single unit (class), and restricting access to some components.
class BankAccount {
private double balance = 1000.0;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
System.out.println("Initial Balance: " + account.getBalance());
account.deposit(500);
System.out.println("Updated Balance: " + account.getBalance());
}
}
Initial Balance: 1000.0
Updated Balance: 1500.0
The balance
variable is private
, meaning it cannot be accessed directly. This ensures data protection. Access is controlled using public methods.
Inheritance allows a class to inherit properties and methods from another class. This promotes code reuse and logical hierarchy.
class Animal {
void makeSound() {
System.out.println("Some generic sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.makeSound(); // inherited method
d.bark(); // specific method
}
}
Some generic sound
Dog barks
The Dog
class extends Animal
. It gains access to the makeSound()
method and also adds its own behavior with bark()
.
Polymorphism allows us to perform a single action in different ways. Java supports two types:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 3)); // int version
System.out.println(calc.add(5.2, 3.8)); // double version
}
}
8
9.0
The method add
is overloaded with different parameter types. This is polymorphism in action—same name, different behavior.
Abstraction hides unnecessary details and shows only the essential features of an object.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle(); // abstract class reference
s.draw();
}
}
Drawing a circle
The abstract class Shape
defines an abstract method draw()
. The Circle
class implements that method, completing the abstraction cycle.
Java OOP is the backbone of writing meaningful Java applications. As you move forward, remember: classes are blueprints, objects are real, and encapsulation, inheritance, polymorphism, and abstraction are the four wheels that keep your code vehicle moving smoothly.
⬅ Previous Topic
Java Continue StatementNext Topic ⮕
Java ClassYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.