Sunday, November 23, 2025

thumbnail

Object-Oriented Programming in Java

 ☕ Object-Oriented Programming in Java


Object-Oriented Programming (OOP) is a programming style where software is built around objects—real-world entities with properties and behaviors.

Java is one of the most widely used OOP languages.


OOP in Java is based on four main pillars:


Encapsulation


Inheritance


Polymorphism


Abstraction


Let’s break them down.


๐Ÿ”ท 1. Classes and Objects

Class


A class is a blueprint or template that defines:


Attributes (variables)


Behaviors (methods)


Example:


class Car {

    String color;

    void drive() {

        System.out.println("The car is driving...");

    }

}


Object


An object is an instance created from a class.


Car myCar = new Car();

myCar.color = "Red";

myCar.drive();


๐Ÿ”ท 2. Encapsulation


Encapsulation means protecting data by keeping variables private and exposing access through public methods.


Benefits:


Data protection


Controlled access


Improved maintainability


Example:


class Person {

    private String name;        // hidden data


    public void setName(String name) {  // setter

        this.name = name;

    }


    public String getName() {  // getter

        return name;

    }

}


๐Ÿ”ท 3. Inheritance


Inheritance allows one class to inherit fields and methods from another class.


Parent class = superclass


Child class = subclass


Example:


class Animal {

    void eat() {

        System.out.println("Eating...");

    }

}


class Dog extends Animal {

    void bark() {

        System.out.println("Barking...");

    }

}



Usage:


Dog d = new Dog();

d.eat();   // inherited

d.bark();  // own behavior



Benefits:


Code reusability


Clear hierarchy


๐Ÿ”ท 4. Polymorphism


Polymorphism means one name, multiple forms.


Two Types:


Compile-time polymorphism → Method Overloading


Runtime polymorphism → Method Overriding


Method Overloading (same name, different parameters)

class MathHelper {

    int add(int a, int b) { return a + b; }

    double add(double a, double b) { return a + b; }

}


Method Overriding (child redefines parent method)

class Animal {

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

}


class Cat extends Animal {

    @Override

    void sound() { System.out.println("Meow"); }

}


๐Ÿ”ท 5. Abstraction


Abstraction means showing only essential features and hiding background details.


Two ways to implement abstraction:


Abstract classes


Interfaces


Abstract Class Example

abstract class Shape {

    abstract void draw();

}


class Circle extends Shape {

    @Override

    void draw() {

        System.out.println("Drawing a circle");

    }

}


Interface Example

interface Animal {

    void makeSound();

}


class Dog implements Animal {

    public void makeSound() {

        System.out.println("Woof!");

    }

}


๐Ÿ”ท 6. OOP in Java: Additional Concepts

✔ Constructors


Used to create and initialize objects.


class Student {

    String name;


    Student(String name) {

        this.name = name;

    }

}


✔ this and super


this → refers to current object


super → refers to parent class


✔ Access Modifiers


public


private


protected


default (package-private)


✔ Packages


Used to organize Java classes.


package com.example;


๐ŸŽฏ Why OOP is Important in Java


Code is modular and easy to maintain


Promotes reusability


Easy to scale and extend


Helps model real-world entities


Improves readabilitya

Learn Full Stack JAVA Course in Hyderabad

Read More

☕ Core Java Concepts

Using Bootstrap in Frontend Development

Routing in Angular – A Beginner’s Guide

Building a Simple Todo App in React

Visit Our Quality Thought Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive