Java OOPs: Mastering Object-Oriented Programming

Java OOPs: Mastering Object-Oriented Programming

Play this article

Greetings, Java aficionados! Welcome back to another enlightening chapter of our Java programming series. We've traversed through the fascinating realms of Java Strings, Classes, and Objects, enhancing our programming prowess. If you've missed any of those articles, now's the perfect time to catch up and elevate your Java journey. In this article, we're embarking on an exciting odyssey into the heart of Object-Oriented Programming (OOP) with Java. Get ready to unravel the intricacies and power of OOP concepts!

1) Introduction to Object-Oriented Programming (OOP)

OOP is a paradigm that structures code around objects, promoting modularity and reusability. We'll embark on an exploration of encapsulation, access modifiers, inheritance, polymorphism, abstraction, and interfaces to demystify the core tenets of OOP.

2) Encapsulation

Encapsulation is a fundamental concept in Object-Oriented Programming (OOP) that revolves around the idea of bundling data (attributes) and methods (functions) that operate on the data into a single unit, known as a class. The primary goal of encapsulation is to create a protective barrier around the internal state of an object, preventing unauthorized access and modification. This promotes data integrity, code maintainability, and flexibility. Let's delve deeper into the concept of encapsulation:

Key Aspects of Encapsulation:

  1. Data Hiding: Encapsulation hides the internal details and state of an object from external entities. Only the methods defined in the class can access and modify the encapsulated data, shielding it from unintended interference.

  2. Access Control: Access modifiers (public, private, protected, default) play a pivotal role in encapsulation. They dictate the level of visibility and access rights for class members. Private members are accessible only within the class, whereas public members can be accessed from any context.

  3. Data Protection: By making attributes private, you ensure that they can be modified only through controlled methods (setter methods) provided by the class. This prevents direct manipulation and ensures data consistency.

Benefits of Encapsulation:

  1. Modularity: Encapsulation promotes modularity by encapsulating related data and behavior within a single unit. This improves code organization, readability, and maintainability.

  2. Code Reusability: Encapsulated classes can be reused in different contexts without worrying about their internal implementation. This fosters code reuse and reduces redundancy.

  3. Flexibility: Encapsulation allows you to modify the internal implementation of a class without affecting the code that uses the class. This enhances flexibility during software development.

Example of Encapsulation:

Consider a BankAccount class that encapsulates the details of a customer's bank account:

public class BankAccount {
    private String accountNumber; // Private attribute
    private double balance;

    public double getBalance() {  // Public method to access balance
        return balance;

    public void deposit(double amount) { // Public method to modify balance
        balance += amount;

In this example, the accountNumber attribute is private, ensuring that it can only be accessed or modified using the public methods provided by the class (getBalance() and deposit()).

Encapsulation and Information Hiding:

Encapsulation is closely related to the concept of information hiding. It allows you to hide the implementation details of a class and expose only the necessary functionalities. This not only protects the integrity of data but also enables changes to the internal implementation without affecting the code that uses the class.

3) Access Modifiers in Java

Access modifiers are keywords in Java that determine the visibility and accessibility of classes, methods, variables, and constructors within a program. They play a crucial role in encapsulation and control the extent to which other parts of the program can interact with a class or its members. Java offers four main types of access modifiers:

  1. public: Members with the public access modifier are accessible from any class in any package. They have the widest scope of visibility and can be freely accessed and used.

  2. private: Members with the private access modifier are only accessible within the class they are declared in. They are not visible or accessible from outside the class, even from subclasses within the same package.

  3. protected: Members with the protected access modifier are accessible within the same package and in subclasses (even if they are in different packages). This modifier is useful for creating a limited form of visibility within the package and promoting controlled inheritance.

  4. default (No Modifier): If no access modifier is specified, the member is given package-private access. This means the member is accessible only within its own package. It is often called the "friendly" access level because it provides visibility within the same package.

Access Modifier Usage:

1. public Access Modifier:

The public access modifier is widely used when you want a class, method, or variable to be accessible from any part of your application. It is often employed for methods that serve as entry points to your classes and for constants that need to be accessed globally.

public class Circle {
    public double calculateArea(double radius) {
        return Math.PI * radius * radius;

2. private Access Modifier:

The private access modifier is used when you want to restrict the visibility of a member to within its own class. It is often applied to instance variables to prevent direct modification and ensure data encapsulation.

public class BankAccount {
    private double balance;

    private void deductTransactionFee() {
        balance -= 0.5;

3. protected Access Modifier:

The protected access modifier is useful when you want to provide visibility within a package and also allow subclasses in other packages to access certain members. It strikes a balance between encapsulation and controlled inheritance.

package animals;

public class Animal {
    protected String name;

    protected void eat() {
        System.out.println(name + " is eating.");

4. default (No Modifier) Access:

The default access level is primarily used for classes, methods, and variables that are meant to be used within the same package. It is the default when no access modifier is specified.

package geometry;

class Rectangle {
    int width;
    int height;

    int calculateArea() {
        return width * height;

4) Static Members in Java

Static members in Java are class-level members that are associated with the class itself, rather than with individual instances (objects) of the class. These members are shared among all instances of the class and can be accessed using the class name. Static members include static variables and static methods. Let's explore the concept of static members in detail:

Key Aspects of Static Members:

  1. Static Variables (Class Variables): Static variables are shared among all instances of a class. They are declared using the static keyword and are stored in a single memory location, regardless of the number of objects created.

  2. Static Methods: Static methods are associated with the class, not with instances. They can be invoked using the class name and can access only static members of the class.

  3. Shared Data: Since static variables are shared, modifying the value of a static variable in one instance affects its value in all other instances.

  4. Global Accessibility: Static members can be accessed from any part of the program using the class name, making them globally accessible.

Benefits of Static Members:

  1. Memory Efficiency: Static variables are stored in a common memory location, saving memory when multiple instances of the class are created.

  2. Utility Methods: Static methods are often used for utility functions that don't require access to instance-specific data. For example, Math class methods like Math.sqrt() are static.

  3. Constants: Static variables are commonly used to define constants that remain unchanged across instances of the class.

Example of Static Members:

Consider a Student class with a static variable totalCount to keep track of the total number of students:

public class Student {
    private static int totalCount; // Static variable

    public Student() {
        totalCount++; // Increment totalCount on each object creation

    public static int getTotalCount() { // Static method
        return totalCount;

In this example, the totalCount variable is shared among all instances of the Student class. The getTotalCount() static method allows you to retrieve the total number of students.

Static Block:

In addition to static variables and methods, Java also allows the use of static blocks. A static block is a block of code that is executed when the class is loaded into memory. It is used to initialize static variables or perform other class-level tasks.

public class Example {
    static int x;

    static {
        x = 10; // Initialize static variable x

When to Use Static Members:

Static members are suitable for situations where data or behavior needs to be shared among all instances of a class. Common scenarios include utility methods, constants, counters, and resources that should be shared among objects.

5) Inheritance in Java

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class to inherit the properties and behaviors (fields and methods) of an existing class. The existing class is referred to as the base class, superclass, or parent class, while the new class is known as the derived class, subclass, or child class. Inheritance forms an "is-a" relationship between classes, where the subclass is a specialized version of the superclass. Let's explore inheritance in Java in detail:

Key Concepts of Inheritance:

  1. Reusability: Inheritance promotes code reuse by allowing the subclass to inherit the attributes and methods of the superclass, eliminating the need to duplicate code.

  2. Extensibility: The subclass can extend the functionality of the superclass by adding new attributes and methods or by overriding existing methods.

  3. Single Inheritance: Java supports single inheritance, which means a class can inherit from only one superclass. This helps maintain a clear and manageable class hierarchy.

  4. Multiple Interfaces: Java supports multiple interfaces, allowing a class to implement multiple interfaces, which enables a form of multiple inheritance through interface implementation.

Syntax of Inheritance:

class Superclass {
    // superclass members

class Subclass extends Superclass {
    // subclass members

Example of Inheritance:

Consider a Shape superclass and subclasses Circle and Rectangle:

class Shape {
    void draw() {
        System.out.println("Drawing a shape");

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle");

class Rectangle extends Shape {
    void draw() {
        System.out.println("Drawing a rectangle");

In this example, both Circle and Rectangle inherit the draw() method from the Shape superclass. However, they provide their own specialized implementation of the draw() method.

Method Overriding:

Subclasses can override (modify) the behavior of methods inherited from the superclass. This allows the subclass to provide its own implementation while maintaining the same method signature.

Access Modifiers and Inheritance:

  • public and protected members: Inherited by the subclass and can be accessed directly.

  • Default (package-private) members: Inherited by the subclass if they are in the same package.

  • private members: Not inherited by the subclass.

The super Keyword:

The super keyword is used to refer to the superclass and its members within the subclass. It is often used to call the superclass's constructor or to access overridden methods.

class Subclass extends Superclass {
    Subclass() {
        super(); // Call the superclass constructor

    void method() {
        super.method(); // Call the superclass method

Constructors and Inheritance:

Subclasses inherit the constructors of the superclass, but they are not directly callable using the super keyword. Instead, the subclass's constructors implicitly call a superclass constructor using super().

6) 'super' Keyword in Java

The 'super' keyword refers to the superclass and is used to invoke superclass methods and constructors. We'll explore its role in facilitating inheritance and method overriding.

class Parent {
    void display() {
        System.out.println("This is the parent class.");

class Child extends Parent {
    void show() {
        System.out.println("This is the child class.");

7) Polymorphism in Java

Polymorphism allows objects of different classes to be treated as objects of a common superclass. We'll delve into method overloading, method overriding, and dynamic method dispatch.

class Shape {
    void draw() {
        System.out.println("Drawing a shape.");

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle.");

8) Abstraction

Abstraction hides complex implementation details and provides a simplified interface. We'll exemplify abstraction using an abstract class.

abstract class Vehicle {
    abstract void start();
    abstract void stop();

class Car extends Vehicle {
    void start() {
        System.out.println("Car started.");

    void stop() {
        System.out.println("Car stopped.");

9) Interface in Java

An interface in Java is a reference type that defines a contract of methods that a class must implement. It allows you to create a blueprint for classes to follow, ensuring that they provide certain methods with specific signatures. Interfaces play a crucial role in achieving multiple inheritance, abstraction, and the design of flexible and modular code. Let's delve into interfaces in Java in detail:

Key Concepts of Interfaces:

  1. Abstraction: Interfaces provide a level of abstraction by defining a set of method signatures without specifying the implementation details. Classes that implement an interface must provide concrete implementations for its methods.

  2. Multiple Inheritance: A class can implement multiple interfaces, allowing it to inherit behavior from multiple sources. This is a way to achieve a form of multiple inheritance in Java.

  3. Polymorphism: Interfaces enable polymorphism, where objects of different classes that implement the same interface can be treated uniformly.

Declaring an Interface:

interface MyInterface {
    void method1(); // Method signatures (implicitly public and abstract)
    int method2();

Implementing an Interface:

A class implements an interface by providing concrete implementations for all the methods declared in the interface.

class MyClass implements MyInterface {
    public void method1() {
        // Implementation of method1

    public int method2() {
        // Implementation of method2

Default and Static Methods:

Starting from Java 8, interfaces can have default and static methods with implementations. Default methods provide a default implementation that can be optionally overridden by implementing classes. Static methods are associated with the interface itself and cannot be overridden.

interface MyInterface {
    void method1();

    default void defaultMethod() {
        // Default implementation

    static void staticMethod() {
        // Static method implementation

Use of Interfaces:

  1. Defining Contracts: Interfaces define a contract that classes must adhere to. They are used to ensure that classes provide specific methods.

  2. Decoupling: Interfaces promote loose coupling by allowing classes to interact through interfaces rather than concrete implementations. This enhances flexibility and modularity.

  3. Achieving Polymorphism: Interfaces enable polymorphism, allowing objects of different classes to be treated uniformly if they implement the same interface.

Real-World Example:

Consider an Animal interface and classes Dog and Cat that implement it:

interface Animal {
    void makeSound();

class Dog implements Animal {
    public void makeSound() {
        System.out.println("Dog barks");

class Cat implements Animal {
    public void makeSound() {
        System.out.println("Cat meows");

Extending Interfaces:

An interface can extend another interface, allowing you to build a hierarchy of interfaces. A class that implements a derived interface must provide implementations for all methods from both interfaces.

interface A {
    void methodA();

interface B extends A {
    void methodB();

class MyClass implements B {
    public void methodA() {
        // Implementation of methodA

    public void methodB() {
        // Implementation of methodB

10) Conclusion

Congratulations on navigating through the intricate landscape of Java Object-Oriented Programming! You've unlocked the power of encapsulation, access modifiers, inheritance, polymorphism, abstraction, and interfaces.

Remember, practice makes perfect. Experiment with encapsulation, inheritance, and polymorphism, and challenge yourself with abstract classes and interfaces. Happy coding, and see you in the next article!

Did you find this article valuable?

Support Mayank Bohra by becoming a sponsor. Any amount is appreciated!