OOPs Concepts in Java
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
| Term | Meaning | Bank example |
|---|---|---|
| Class | A blueprint: which data and actions every object of this kind has | BankAccount: every account has a holder and a balance, and can deposit and withdraw |
| Object | One real thing built from the blueprint, with its own data | Navika's account with Rs. 5,000; Srihitha's account with Rs. 12,000 |
| Field (state) | A piece of data each object holds | holder, balance |
| Method (behavior) | An action an object can do | deposit(500), withdraw(200) |
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());
}
}Navika: Rs. 6500
Srihitha: Rs. 12000How it works
- Lines 2 to 18 define the class
BankAccount: two fields, a constructor that fills them in, and two methods. - Lines 21 and 22 create two objects with
new. Each has its ownholderandbalance. - 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
| Pillar | Idea in one line | Bank example | Module |
|---|---|---|---|
| Encapsulation | Keep data private; change it only through methods that check the rules | balance is private; withdraw refuses to go below zero | 13 |
| Inheritance | A new class reuses and extends an existing one | SavingsAccount extends BankAccount | 10 |
| Polymorphism | One call, many behaviors, depending on the actual object | account.interest() works differently for savings and current accounts | 11 |
| Abstraction | Show what something does; hide how | An ATM shows "Withdraw"; the network calls behind it are hidden | 12 |
This chapter, OOP Fundamentals, builds the foundation: classes, objects, constructors, this, static and final. The next four modules take one pillar each.
Why OOP?
| Benefit | What it means for you |
|---|---|
| Models the real world | Code is organized around things people already understand: accounts, tickets, orders |
| Protects data | Rules live next to the data they protect, so they can't be skipped |
| Reuse | Inheritance and composition let new classes build on tested ones |
| Easier change | Code that uses an object doesn't need to know how it works inside |
| Teamwork | Different 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
- Easy: List three fields and two methods for a
Ticketclass in a railway app. - Harder: For each of the four pillars, write one sentence about how it could apply to a food delivery app.
Key Takeaways
Was this tutorial helpful?