Object Oriented Programming

CORE JAVA

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which are instances of classes, and the interactions between these objects. OOP promotes the organization of code into manageable and reusable components, leading to more modular, flexible, and maintainable software systems.

Here's a detailed explanation of OOP principles with examples:

1.Classes and Objects:

  • A class is a blueprint or template for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of that class will have. For example, consider a class named

    public class Car {

    // Attributes

    String make;

    String model;

    int year;

    // Constructor

    public Car(String make, String model, int year) {

    this.make = make;

    this.model = model;

    this.year = year;

    }

    // Method

    public void start() {

    System.out.println("The " + make + " " + model + " is starting.");

    }

    }

  • In this example, Car is a class that has attributes (make, model, year) and a method (start()).

    An object is an instance of a class. It represents a specific instance of the class and can have its own state (attribute values) and behavior (method invocations). For example:

    public class Main {

    public static void main(String[] args) {

    // Creating objects of the Car class

    Car car1 = new Car("Toyota", "Camry", 2022);

    Car car2 = new Car("Honda", "Accord", 2023);

    // Accessing attributes and invoking methods

    System.out.println(car1.make); // Output: Toyota

    car2.start(); // Output: The Honda Accord is starting.

    }

    }

2. Encapsulation:

  • Encapsulation refers to the bundling of data (attributes) and methods that operate on that data within a single unit (class). It hides the internal state of an object and only exposes a public interface for interacting with it. This protects the integrity of the data and promotes modular design. In Java, encapsulation is achieved using access modifiers (public, private, protected) to control access to class members.

3.Inheritance:

  • Inheritance is a mechanism where a new class (subclass or derived class) inherits properties and behaviors from an existing class (superclass or base class). It promotes code reuse and establishes a relationship between classes. Subclasses can extend the functionality of the superclass by adding new methods or overriding existing ones. For example:

  • public class ElectricCar extends Car {

    // Additional attribute

    int batteryCapacity;

    // Constructor

    public ElectricCar(String make, String model, int year, int batteryCapacity) {

    super(make, model, year); // Call to superclass constructor

    this.batteryCapacity = batteryCapacity;

    }

    // Additional method

    public void charge() {

    System.out.println("The " + make + " " + model + " is charging.");

    }

    }

4.Polymorphism:

  • Polymorphism allows objects to be treated as instances of their parent class or as instances of their subclass. It enables methods to do different things based on the object they are called on. Polymorphism is achieved through method overriding (subclass provides a specific implementation of a method defined in the superclass) and method overloading (multiple methods with the same name but different parameter lists). For example:

  • public class Main {

    public static void main(String[] args) {

    Car car1 = new Car("Toyota", "Camry", 2022);

    ElectricCar car2 = new ElectricCar("Tesla", "Model S", 2024, 100);

    car1.start(); // Output: The Toyota Camry is starting.

    car2.start(); // Output: The Tesla Model S is starting.

    car2.charge(); // Output: The Tesla Model S is charging.

    }

    }

5.Abstraction :
Abstraction is a fundamental concept in object-oriented programming (OOP) that focuses on hiding the complex implementation details of a class while exposing only the necessary features to the outside world. In Java, abstraction is achieved through abstract classes and interfaces. For example we can use features of ATM machine such as cash withdrawal, checking balance , etc. without knowing how ATM machine works internally.

These are the fundamental concepts of Object-Oriented Programming in Java. By applying these principles effectively, developers can design robust, maintainable, and scalable software systems.