Java OOP Introduction
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: