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

Java OOP Introduction

Object-oriented programming organizes code like real-world things, each with facts and actions, such as a dog with a name that can bark. It keeps large programs neat.

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
// Define the class Account
class Account {
	private double balance;
	public double getBalance() {
		// Send balance back to the caller
		return balance;
	}
	public void deposit(double amount) {
		balance += amount;
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Account object and store it in acc
		Account acc = new Account();
		acc.deposit(100);
		// Print a line to the console
		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
// Define the class Shape
abstract class Shape {
	abstract double area();
}
// Define the class Circle
class Circle extends Shape {
	// Declare radius and set it to 2
	double radius = 2;
	double area() {
		// Send Math.PI * radius * radius back to the caller
		return Math.PI * radius * radius;
	}
}
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Circle object and store it in s
		Shape s = new Circle();
		// Print a line to the console
		System.out.println(s.area());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Confusing a class with an object, when the class is the blueprint and objects are instances.
  2. Making all fields public instead of using encapsulation.
  3. Using inheritance where composition would fit better.

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.