Oops in Java

Object-Oriented Programming (OOP) is a programming paradigm that provides a structured approach to designing and implementing software systems. Java is a popular language that fully supports OOP principles. In this article, we will explore the key concepts of OOP in Java, including classes, objects, inheritance, polymorphism, encapsulation, and abstraction.

Classes and Objects

In Java, a class is a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that an object of that class can have. Objects are instances of a class and represent real-world entities. Here's the syntax for creating a class and an object in Java.

// Class declaration
class ClassName {
    // Fields (attributes)
    // Constructors
    // Methods
}
// Object creation
ClassName objectName = new ClassName();
        
Inheritance

Inheritance is a mechanism in Java that allows a class to inherit the properties and methods of another class. The class that is inherited from is called the superclass or parent class, while the class that inherits is called the subclass or child class. Inheritance promotes code reuse and supports the "is-a" relationship. Here's an example of inheritance in Java.

// Parent class
class Vehicle {
    void start() {
        System.out.println("Vehicle started");
    }
}
// Child class inheriting from Vehicle
class Car extends Vehicle {
    void accelerate() {
        System.out.println("Car accelerated");
    }
}
// Creating an object of the Car class
Car car = new Car();
car.start();        // Output: Vehicle started
car.accelerate();   // Output: Car accelerated
        
Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables you to write flexible and extensible code. Polymorphism is achieved through method overriding and method overloading. Here's an example of polymorphism using method overriding.

// Parent class
class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}
// Child class overriding makeSound() method
class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}
// Creating objects of Animal and Dog classes
Animal animal = new Animal();
Dog dog = new Dog();
// Polymorphic method invocation
animal.makeSound();   // Output: Animal makes a sound
dog.makeSound();      // Output: Dog barks
        
Encapsulation

Encapsulation is a principle that bundles data and methods together within a class, hiding the internal implementation details from the outside world. It provides data protection and ensures code modularity. The access modifiers (public, private, protected) are used to control the visibility of class members. Here's an example demonstrating encapsulation.

class Employee {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
// Creating an object of the Employee class
Employee employee = new Employee();
employee.setName("John Doe");
employee.setAge(30);
System.out.println("Name: " + employee.getName());
System.out.println("Age: " + employee.getAge());
        
Abstraction

Abstraction is a concept that focuses on displaying essential features while hiding unnecessary details. In Java, abstraction is achieved through abstract classes and interfaces. An abstract class cannot be instantiated and can contain abstract methods (methods without implementation) along with regular methods. Interfaces define a contract that classes can implement. Here's an example of abstraction using an abstract class and interface.

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

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

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

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

// Creating objects and invoking draw() method
Shape shape = new Circle();
shape.draw();       // Output: Drawing a circle

Drawable drawable = new Rectangle();
drawable.draw();    // Output: Drawing a rectangle
        
Note

Object-Oriented Programming is a powerful paradigm that promotes code reusability, modularity, and flexibility. Java's support for OOP concepts like classes, objects, inheritance, polymorphism, encapsulation, and abstraction makes it a popular choice for developing robust and scalable applications.