Introduction to Object-Oriented Programming (OOP)
Java is an object-oriented programming language, which means it is based on the concept of objects. Objects represent real-world entities and encapsulate data and behaviors into a single unit. This programming paradigm promotes modularity, reusability and scalability.
Java OOP revolves around four main principles:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Key Concepts of Java OOP
1. Class
A class is a blueprint or template for creating objects. It defines the properties (fields) and behaviors (methods) that the objects of that class will have.
Example:
class Car {
// Fields
String brand;
int year;
// Method
void displayDetails() {
System.out.println("Brand: " + brand + ", Year: " + year);
}
}
2. Object
An object is an instance of a class. It has a state (defined by fields) and behavior (defined by methods).
Example:
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Creating an object
myCar.brand = "Tesla";
myCar.year = 2023;
myCar.displayDetails(); // Calling a method
}
}
Output:
Brand: Tesla, Year: 2023
Principles of Java OOP
1. Encapsulation
Encapsulation is the process of wrapping data (fields) and code (methods) into a single unit. It ensures data security by restricting direct access to fields and requiring interaction through methods.
Key Points:
- Fields are typically marked as private.
- Getter and setter methods control access to fields.
Example:
class Person {
private String name;
// Getter method
public String getName() {
return name;
}
// Setter method
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("John");
System.out.println("Name: " + person.getName());
}
}
Output:
Name: John
2. Inheritance
Inheritance allows a class (child class) to inherit properties and methods from another class (parent class). It promotes reusability and establishes a relationship between classes.
Key Points:
- Use the extends keyword.
- The child class can override parent methods.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("This dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Method of Dog class
}
}
Output:
This animal eats food.
This dog barks.
3. Polymorphism
Polymorphism means “many forms.” It allows one action to behave differently based on the object performing it. Polymorphism can be achieved through method overloading and method overriding.
Method Overloading Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum: " + calc.add(5, 10));
System.out.println("Sum: " + calc.add(5, 10, 15));
}
}
Output:
Sum: 15
Sum: 30
Method Overriding Example:
class Animal {
void sound() {
System.out.println("This animal makes a sound.");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("This cat meows.");
}
}
public class Main {
public static void main(String[] args) {
Animal myCat = new Cat();
myCat.sound(); // Calls the overridden method
}
}
Output:
This cat meows.
4. Abstraction
Abstraction hides implementation details and shows only the essential features of an object. It is implemented using abstract classes and interfaces.
Abstract Class Example:
abstract class Shape {
abstract void draw(); // Abstract method
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle.");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}
Output:
Drawing a circle.
Interface Example:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound();
}
}
Output:
The dog barks.
Benefits of OOP in Java
- Modularity: Code is easier to manage and modify.
- Reusability: Reuse existing code with inheritance.
- Flexibility: Polymorphism allows different implementations with the same interface.
- Security: Encapsulation protects sensitive data.
Common Applications of OOP
- Game Development: Objects like players, weapons and environments.
- GUI Applications: Buttons, forms and events are modeled as objects.
- Real-World Modeling: Systems like banking, inventory management and e-commerce.