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.
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.
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.
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.
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.
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 class | Interface |
|---|---|
| Can have fields with state | Only constants (static final) |
| Constructors allowed | No constructors |
| A class extends only one | A class can implement many |
| Use for shared base code | Use 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 withsuper.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.
