Object-Oriented Programming (OOP) in Java - Placement Preparation
Definition of OOP
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
'objects', which can contain data (attributes) and code (methods).
4 Pillars of OOP
1. Encapsulation - Wrapping data and methods into a single unit (class) and restricting
access.
Example:
class Student {
private int age;
public void setAge(int a) { age = a; }
public int getAge() { return age; }
}
2. Abstraction - Hiding complex implementation and showing only essential features.
Example:
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() { System.out.println("Car is starting"); }
}
3. Inheritance - One class inherits properties from another class.
Example:
class Animal {
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("Barking..."); }
}
4. Polymorphism - Ability of an object to take many forms (overloading and overriding).
Overloading Example:
class Math {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
Overriding Example:
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Cat extends Animal {
void sound() { System.out.println("Meow"); }
}
Class and Object
Class is a blueprint. Object is an instance of a class.
Example:
class Car {
String color = "Red";
void run() { System.out.println("Car is running"); }
}
Constructors
Constructors are special methods to initialize objects.
Types: Default, Parameterized
Example:
Student(String n) { name = n; }
Interface vs Abstract Class
Interface: Only abstract methods (Java 8+ supports default methods), multiple inheritance
supported.
Abstract Class: Can have both abstract and concrete methods.
Example:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() { System.out.println("Bark"); }
}
Exception Handling
Handle runtime errors using try-catch-finally.
Types: Checked and Unchecked
Example:
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Finally block");
}
Threading
Used for concurrent execution.
Example:
class MyThread extends Thread {
public void run() { System.out.println("Thread is running"); }
}
Important Java Keywords
this, super, final, static, abstract, synchronized, transient, volatile, instanceof, try, catch,
finally, throw, throws
Differences
Class vs Object: Blueprint vs Instance
Abstraction vs Encapsulation: Hiding complexity vs Binding data
Overloading vs Overriding: Compile-time vs Runtime
Top OOP Interview Questions
1. What are the main principles of OOP?
2. Difference between Abstract Class and Interface?
3. What is method overloading vs method overriding?
4. Can we achieve multiple inheritance in Java?
5. What is the use of final keyword in Java?
6. What are access modifiers in OOP?
7. Explain constructor types with examples.
8. What is the difference between static and instance methods?
9. What is the difference between class and object?
10. Explain try-catch-finally block with example.