APEX Educational Institute

Java OOPs Concepts with Examples (Class, Object, Inheritance, Polymorphism)

The four pillars of Object-Oriented Programming in Java (encapsulation, inheritance, polymorphism and abstraction) explained with simple code examples and common interview questions.

Beginner | 3 min read | Updated

Java is an object-oriented language: you model a program as objects that hold data (fields) and behaviour (methods). Almost every Java interview starts with OOPs, and every Spring Boot project uses these ideas daily.

Class and object

A class is a blueprint. An object is a real instance created from that blueprint.

java
public class Student {
    String name;
    int marks;

    void printResult() {
        System.out.println(name + " scored " + marks);
    }
}

Student s = new Student();   // object
s.name = "Anitha";
s.marks = 86;
s.printResult();

1. Encapsulation

Keep fields private and expose controlled access through methods. This protects the object from invalid data.

java
public class BankAccount {
    private double balance;

    public double getBalance() { return balance; }

    public void deposit(double amount) {
        if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
        balance += amount;
    }
}

No outside code can set balance = -500 directly; every change goes through deposit, which validates it.

2. Inheritance

A child class reuses and extends a parent class using extends. It models an is-a relationship.

java
class Employee {
    String name;
    double baseSalary;
    double salary() { return baseSalary; }
}

class Manager extends Employee {
    double bonus;
    @Override
    double salary() { return baseSalary + bonus; }
}

Java supports single inheritance for classes (one parent), but a class can implement many interfaces.

3. Polymorphism

One reference type, many behaviours.

  • Compile-time (overloading): same method name, different parameters.
  • Runtime (overriding): child class replaces a parent method; the JVM picks the version based on the actual object.
java
class Calculator {
    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }   // overloading
}

Employee e = new Manager();   // parent reference, child object
e.salary();                   // calls Manager.salary() at runtime (overriding)

4. Abstraction

Show what an object does and hide how. Use abstract classes or interfaces.

java
interface PaymentGateway {
    boolean pay(double amount);
}

class UpiPayment implements PaymentGateway {
    public boolean pay(double amount) {
        System.out.println("Paid Rs " + amount + " via UPI");
        return true;
    }
}

PaymentGateway gateway = new UpiPayment();
gateway.pay(1500);

The calling code depends only on PaymentGateway. You can switch to a card gateway without changing it. Spring Boot's dependency injection is built on exactly this idea.

Abstract class vs interface

Abstract classInterface
Can have fields with stateOnly constants (static final)
Constructors allowedNo constructors
A class extends only oneA class can implement many
Use for shared base codeUse for a capability or contract

Since Java 8, interfaces can also have default and static methods.

Common interview questions

  • Can we override a static method? No. Static methods are hidden, not overridden, because they belong to the class, not the object.
  • What is `super`? It refers to the parent class: call its constructor with super(...) or its method with super.method().
  • Why is multiple inheritance not allowed for classes? To avoid the "diamond problem", where two parents have the same method and the child cannot decide which to use.
  • Composition vs inheritance? Composition (has-a) puts one object inside another; prefer it when the relationship is not truly is-a.

Next steps

Practise by modelling a small library system with Book, Member and Loan classes. Then move on to building a REST API with Spring Boot, or join the Java + Spring Boot + AI course.

Master it hands-on

Development + AI

Java + Spring Boot + AI

Java, Spring Boot, Hibernate, REST APIs, microservices and React, with AI.

26 weeks Beginner to Advanced
Online LiveRecorded Course

More Development + AI tutorials