articles

Home / DeveloperSection / Articles / Understanding Object-Oriented Programming in Java, Core Concepts and Implementation

Understanding Object-Oriented Programming in Java, Core Concepts and Implementation

Understanding Object-Oriented Programming in Java, Core Concepts and Implementation

Ravi Vishwakarma 113 03-Jun-2024

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. Java is a widely used OOP language, and its OOP features are fundamental to the language itself. 

Here's an overview of the key concepts of OOP in Java:

 

1. Classes and Objects

  • Class: A blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit.
  • Object: An instance of a class. When a class is defined, no memory is allocated until an object of that class is created.
class Car {
    // fields 
    String color;
    String model;
    // methods 
    void drive() {
        System.out.println("The car is driving.");
    }
    void stop() {
        System.out.println("The car has stopped.");
    }
}
public class Main {
    public static void main(String[] args) {
        // Creating an object of Car class 
        Car myCar = new Car();
        myCar.color = "Red";
        myCar.model = "Toyota";
        // Using methods of the Car class 
        myCar.drive();
        myCar.stop();
    }
}

2. Inheritance

Inheritance allows a new class to inherit properties and behavior from an existing class.

class Vehicle {
    void start() {
        System.out.println("Vehicle is starting.");
    }
}
class Car extends Vehicle {
    void drive() {
        System.out.println("Car is driving.");
    }
}
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.start();
        // Inherited method 
        myCar.drive(); // Method of Car class 
    }
} 

3. Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon, even though they share the same name. It can be achieved through method overloading (compile-time polymorphism) and method overriding (runtime polymorphism).

  • Method Overloading: Multiple methods in the same class with the same name but different parameters.
class MathUtils {
    int add(int a, int b) {
        return a + b;
    }
    int add(int a, int b, int c) {
        return a + b + c;
    }
}
  • Method Overriding: A subclass provides a specific implementation of a method that is already defined in its superclass.
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}
class Dog extends Animal {
    @Override void sound() {
        System.out.println("Dog barks");
    }
}
public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();
        myAnimal.sound(); // Dog barks 
    }
}

4. Encapsulation

Encapsulation is the mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. It restricts direct access to some of an object's components and can prevent the accidental modification of data.

class Person {
    private String name;
    private int age; // Getter and setter methods 
    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;
    }
}
public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        p.setName("John");
        p.setAge(30);
        System.out.println(p.getName());
        System.out.println(p.getAge());
    }
}

5. Abstraction

Abstraction is the concept of hiding the internal details and showing only the functionalities. It can be achieved using abstract classes and interfaces.

  • Abstract Class: A class that cannot be instantiated, and is meant to be subclassed. It can have abstract methods (without body) and concrete methods (with body).
abstract class Shape {
    abstract void draw();
}
class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}
public class Main {
    public static void main(String[] args) {
        Shape shape = new Circle();
        shape.draw(); // Drawing Circle 
    }
}
  • Interface: A reference type in Java, it is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
interface Animal {
    void eat();
}
class Dog implements Animal {
    public void eat() {
        System.out.println("Dog eats");
    }
}
public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.eat(); // Dog eats 
    }
}

Conclusion

The principles of OOP—encapsulation, inheritance, polymorphism, and abstraction—are essential for creating robust, maintainable, and scalable software. Java, with its strong OOP foundation, provides the tools and features to implement these principles effectively. Understanding and applying these concepts allows developers to build well-structured and reusable code.

Thanks for reading.


Updated 03-Jun-2024
Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By