OOPs Concepts in Java — Java Technical Interview Questions & Answers
OOPs Concepts in Java
OOPs Concepts in Java
- OOP means you build software from objects, not from 200 loose variables. An object keeps data and actions together. A BankAccount has balance and also deposit(). A Student has name and also study(). That is the whole idea. A class is the blueprint on paper. new makes one real object from that blueprint.
- Four pillars — don’t only chant the names. Encapsulation: keep balance private; only deposit() can change it. Inheritance: Dog extends Animal and reuses eat(). Polymorphism: speak() is bark for Dog and meow for Cat. Abstraction: startEngine() hides the messy fuel work. One everyday word each, or the interviewer will stop you.
- Let’s take BankAccount on the board. Class has private double balance. Method deposit(100) adds only if amt > 0. getBalance() returns the number. Outside code cannot write acc.balance = -99. That one example is encapsulation + class + object together. Draw it once in your notebook.
- Companies use OOP because a big project is easier to grow when each piece has a clear job. You can test BankAccount without opening the whole website. You can reuse Student in two screens. If everything is public fields in main, one change breaks ten places.
- Learn class and object first, then encapsulation, then inheritance, then polymorphism, then abstract class vs interface. If you skip to interface on day one, you will only memorise implements. Walk the order like a class timetable.
Class: BankAccount (blueprint)
│ new
▼
Object: acc1 (real account)
balance (private)
deposit() / getBalance()Technical Interview Tip: Four pillars × one line × one example word. Don’t only recite names.
Code Examples for OOPs Concepts in Java
Example
class BankAccount {
private int balance;
BankAccount(int start) { this.balance = start; }
void deposit(int amt) { if (amt > 0) balance += amt; }
int getBalance() { return balance; }
}
public class Main {
public static void main(String[] args) {
BankAccount a = new BankAccount(100);
a.deposit(50);
System.out.println(a.getBalance());
}
}OOPs Concepts in Java — output: 150. Start 100 + deposit 50. balance is private — that is encapsulation, one OOP pillar.
How to Explain OOPs Concepts in Java to an Interviewer
What is OOP?
Build software from objects Data and actions stay together
Class vs object?
Class = blueprint Object = real instance via new
Name the four pillars.
Encapsulation Inheritance Polymorphism Abstraction
Give one example for encapsulation.
private balance deposit() changes it
Give one example for inheritance.
Dog extends Animal