CodeToLive

Object-Oriented Programming (OOP) in Java

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure software. Java is a fully object-oriented language, and OOP concepts are fundamental to writing clean, modular, and reusable code.

Core Principles of OOP

Classes and Objects

A class is a blueprint for creating objects. An object is an instance of a class.


// Class Definition
class Car {
    // Fields (Attributes)
    String brand;
    String model;
    int year;

    // Constructor
    public Car(String brand, String model, int year) {
        this.brand = brand;
        this.model = model;
        this.year = year;
    }

    // Method
    void displayInfo() {
        System.out.println("Brand: " + brand + ", Model: " + model + ", Year: " + year);
    }
}

// Creating an Object
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Toyota", "Corolla", 2020);
        myCar.displayInfo();
    }
}
      

Encapsulation

Encapsulation is achieved by declaring fields as private and providing public getter and setter methods.


class Person {
    private String name;
    private int age;

    // Getter for name
    public String getName() {
        return name;
    }

    // Setter for name
    public void setName(String name) {
        this.name = name;
    }

    // Getter for age
    public int getAge() {
        return age;
    }

    // Setter for age
    public void setAge(int age) {
        if (age > 0) {
            this.age = age;
        }
    }
}
      

Inheritance

Inheritance allows a class to inherit fields and methods from another class. The extends keyword is used.


// Parent Class
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

// Child Class
class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.eat(); // Inherited method
        myDog.bark(); // Child class method
    }
}
      

Polymorphism

Polymorphism allows methods to behave differently based on the object that calls them. It is achieved through method overriding.


class Animal {
    void sound() {
        System.out.println("Animal makes a sound.");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks.");
    }
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("Cat meows.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();
        myDog.sound(); // Dog barks
        myCat.sound(); // Cat meows
    }
}
      

Abstraction

Abstraction is achieved using abstract classes and interfaces. Abstract classes cannot be instantiated, and interfaces define a contract for classes.


// Abstract Class
abstract class Shape {
    abstract void draw(); // Abstract method
}

// Concrete Class
class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a circle.");
    }
}

// Interface
interface Drawable {
    void draw();
}

class Rectangle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle.");
    }
}
      

Interfaces

Interfaces are used to achieve full abstraction and multiple inheritance in Java.


interface Animal {
    void eat();
}

interface Pet {
    void play();
}

class Dog implements Animal, Pet {
    @Override
    public void eat() {
        System.out.println("Dog eats food.");
    }

    @Override
    public void play() {
        System.out.println("Dog plays with a ball.");
    }
}
      
Next: Advanced OOP Concepts