← Back to Core Java Course | Chapter 7: OOP Core | Lesson 1 of 11

Java OOP Introduction

The OOP Concept

Object-Oriented Programming organizes code around classes (blueprints) and objects (instances built from those blueprints) instead of a flat sequence of procedures, which makes it easier to model real-world entities and keep related data and behavior bundled together.

Example: The OOP Concept

java
class Car {
	String model;
}
public class Main {
	public static void main(String[] args) {
		Car myCar = new Car(); // an object built from the Car class blueprint
		myCar.model = "Sedan";
		System.out.println(myCar.model);
	}
}

Encapsulation

Encapsulation hides a class's internal data behind private fields, exposing controlled access only through public getter and setter methods. This means the class can change how it stores data internally without breaking any code that uses it through its public methods.

Example: Encapsulation

java
class Account {
	private double balance;
	public double getBalance() {
		return balance;
	}
	public void deposit(double amount) {
		balance += amount;
	}
}
public class Main {
	public static void main(String[] args) {
		Account acc = new Account();
		acc.deposit(100);
		System.out.println(acc.getBalance());
	}
}

Inheritance

Inheritance lets one class acquire the fields and methods of another via extends, avoiding duplicate code across related classes — a Car class inheriting from Vehicle automatically gets everything Vehicle already defines, and only needs to add what's specific to cars.

Example: Inheritance

java
class Vehicle {
	void move() {
		System.out.println("Moving");
	}
}
class Car extends Vehicle {
}
public class Main {
	public static void main(String[] args) {
		Car myCar = new Car();
		myCar.move(); // inherited from Vehicle
	}
}

Polymorphism

Polymorphism ('many forms') lets a single method call behave differently depending on which actual object it's invoked on at runtime, so code written against a general type (like Animal) can correctly call the right behavior for whatever specific subclass (Dog, Cat) is actually passed in.

Example: Polymorphism

java
class Animal {
	void sound() {
		System.out.println("Some sound");
	}
}
class Dog extends Animal {
	void sound() {
		System.out.println("Bark");
	}
}
public class Main {
	public static void main(String[] args) {
		Animal a = new Dog();
		a.sound(); // Bark, decided at runtime
	}
}

Abstraction

Abstraction hides implementation complexity behind a simpler interface, using abstract classes or interfaces to define what a class must do without dictating exactly how — callers interact with the simplified contract and don't need to know the underlying details.

Example: Abstraction

java
abstract class Shape {
	abstract double area();
}
class Circle extends Shape {
	double radius = 2;
	double area() {
		return Math.PI * radius * radius;
	}
}
public class Main {
	public static void main(String[] args) {
		Shape s = new Circle();
		System.out.println(s.area());
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.