OOPs Concepts in Java

Naveen AnamaneniUpdated · 5 min read
easyJava 25javaoopinterview

Think about a bank. It has accounts, customers, cards and transactions. Each account knows its own balance and can do things: accept a deposit, allow a withdrawal, show a statement. Object-oriented programming (OOP) writes software the same way: as a set of objects, each holding its own data and the actions that work on that data. Java is built around this idea, and Level 2 of this course is all about it.

Class and Object: The Foundation

TermMeaningBank example
ClassA blueprint: which data and actions every object of this kind hasBankAccount: every account has a holder and a balance, and can deposit and withdraw
ObjectOne real thing built from the blueprint, with its own dataNavika's account with Rs. 5,000; Srihitha's account with Rs. 12,000
Field (state)A piece of data each object holdsholder, balance
Method (behavior)An action an object can dodeposit(500), withdraw(200)
Bank.java▶ Try it
public class Bank {
    static class BankAccount {
        String holder;
        long balance;
 
        BankAccount(String holder, long balance) {
            this.holder = holder;
            this.balance = balance;
        }
 
        void deposit(long amount) {
            balance += amount;
        }
 
        String summary() {
            return holder + ": Rs. " + balance;
        }
    }
 
    public static void main(String[] args) {
        BankAccount navika = new BankAccount("Navika", 5000);
        BankAccount srihitha = new BankAccount("Srihitha", 12000);
        navika.deposit(1500);
        System.out.println(navika.summary());
        System.out.println(srihitha.summary());
    }
}
Output
Navika: Rs. 6500
Srihitha: Rs. 12000

How it works

  1. Lines 2 to 18 define the class BankAccount: two fields, a constructor that fills them in, and two methods.
  2. Lines 21 and 22 create two objects with new. Each has its own holder and balance.
  3. Line 23 deposits into Navika's account only; Srihitha's balance is untouched.

Try it yourself: add a withdraw(long amount) method and take Rs. 2,000 out of Srihitha's account.

The Four Pillars

Four pillars, built on classes and objects
PillarIdea in one lineBank exampleModule
EncapsulationKeep data private; change it only through methods that check the rulesbalance is private; withdraw refuses to go below zero13
InheritanceA new class reuses and extends an existing oneSavingsAccount extends BankAccount10
PolymorphismOne call, many behaviors, depending on the actual objectaccount.interest() works differently for savings and current accounts11
AbstractionShow what something does; hide howAn ATM shows "Withdraw"; the network calls behind it are hidden12

This chapter, OOP Fundamentals, builds the foundation: classes, objects, constructors, this, static and final. The next four modules take one pillar each.

Why OOP?

BenefitWhat it means for you
Models the real worldCode is organized around things people already understand: accounts, tickets, orders
Protects dataRules live next to the data they protect, so they can't be skipped
ReuseInheritance and composition let new classes build on tested ones
Easier changeCode that uses an object doesn't need to know how it works inside
TeamworkDifferent people can work on different classes

Common Mistakes

Thinking OOP means "put everything in classes". Java requires classes, but OOP is about objects that own their data and behavior, not about wrapping procedural code in a class.

Confusing class and object. "I created a class Navika" is wrong; you create an object of class BankAccount for Navika.

Learning the four pillars as definitions only. Each one solves a real problem; the next modules show which.

Interview Corner

What are the main principles of OOP?

Encapsulation (keeping data private and exposing it through methods), inheritance (a class reusing and extending another), polymorphism (one interface, many behaviors, chosen by the actual object at runtime) and abstraction (showing what an object does while hiding how). They all rest on classes and objects.

Is Java a pure object-oriented language?

No. Primitive types such as int and boolean are not objects, and static methods can be called without any object. Everything else is organized around classes and objects.

Practice

  1. Easy: List three fields and two methods for a Ticket class in a railway app.
  2. Harder: For each of the four pillars, write one sentence about how it could apply to a food delivery app.

Quick check: OOPs concepts

4 questions
1What is a class?
2Which pillar keeps an account's balance private and changes it only through methods?
3SavingsAccount extends BankAccount is an example of which pillar?
4Why is Java not a pure object-oriented language?

Key Takeaways

Was this tutorial helpful?

Lesson 91 of 482