Comprehensive OOP Notes For Technical Interview Preparation (Using Java)
Comprehensive OOP Notes For Technical Interview Preparation (Using Java)
Comprehensive OOP Notes For Technical Interview Preparation (Using Java)
Table of Contents
1. Introduction to OOP
2. Classes and Objects
3. Four Pillars of OOP
○ Encapsulation
○ Abstraction
○ Inheritance
○ Polymorphism
4. Access Modifiers in Java
5. Constructors and Garbage Collection
6. Static Members
7. Method Overloading and Overriding
8. Interfaces and Abstract Classes
9. Association, Aggregation, and Composition
10. Design Patterns
11. SOLID Principles
12. Advanced Topics
13. Interview Tips
1. Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm centered around objects
rather than actions. It allows developers to create modular, reusable code by modeling
real-world entities as software objects that have data fields and associated procedures known
as methods.
Example in Java:
public class Car {
// Attributes (state)
private String brand;
private String color;
// Constructor
public Car(String brand, String color) {
this.brand = brand;
this.color = color;
}
// Methods (behavior)
public void drive() {
"."); System.out.println("Driving the " + color + " " + brand +
}
Definition: Encapsulation is the mechanism of wrapping the data (variables) and the code
acting on the data (methods) together as a single unit, and restricting direct access to some of
an object's components.
Key Points:
Example in Java:
public class BankAccount {
private double balance = 0.0;
// Usage
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(1000.0);
}
}
b. Abstraction
Definition: Abstraction is the concept of hiding the complex reality while exposing only the
necessary parts. It focuses on the essential qualities rather than the specific characteristics of
one particular example.
Key Points:
@Override
double area() {
return Math.PI * radius * radius;
}
}
// Usage
}
c. Inheritance
Definition: Inheritance allows a new class to inherit properties and methods from an existing
class. The new class is called the subclass (child class), and the existing class is called the
superclass (parent class).
Key Points:
Example in Java:
class Animal {
public void eat() {
System.out.println("Eating...");
}
}
// Usage
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Dog's own method
}
}
d. Polymorphism
Definition: Polymorphism allows objects to be treated as instances of their parent class rather
than their actual class. The same method can perform different functions based on the object
that it is acting upon.
Types of Polymorphism:
● Achieved by defining multiple methods with the same name but different parameter lists
within the same class.
● The compiler determines which method to call based on the method signature.
Example in Java:
class Calculator {
// Method Overloading
public int add(int a, int b) {
return a + b;
}
// Usage
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 3)); // Outputs: 8
System.out.println(calc.add(2.5, 3.5)); // Outputs: 6.0
System.out.println(calc.add(1, 2, 3)); // Outputs: 6
}
}
Example in Java:
class Shape {
public void draw() {
System.out.println("Drawing a shape.");
}
}
// Usage
public class Main {
public static void main(String[] args) {
Shape shape1 = new Shape();
Shape shape2 = new Rectangle();
Example in Java:
public class AccessModifiersDemo {
public String publicVar = "Public";
protected String protectedVar = "Protected";
String defaultVar = "Default";
private String privateVar = "Private";
System.out.println(publicVar); // Accessible
System.out.println(protectedVar); // Accessible
class OtherClass {
public void testAccess() {
System.out.println(obj.publicVar); // Accessible
}
5. Constructors and Garbage Collection
Constructors
Example in Java:
public class Person {
private String name;
// Constructor
public Person(String name) {
this.name = name;
}
// Method
public void greet() {
System.out.println("Hello, my name is " + name + ".");
}
}
// Usage
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice");
person.greet(); // Outputs: Hello, my name is Alice.
}
}
Garbage Collection
Example in Java:
public class MathHelper {
public static final double PI = 3.14159;
// Usage
}
7. Method Overloading and Overriding
Method Overloading
● Multiple methods in the same class with the same name but different parameters.
● Compile-time polymorphism.
Example in Java:
public class Printer {
public void print(String text) {
System.out.println(text);
}
// Usage
public class Main {
public static void main(String[] args) {
Printer printer = new Printer();
printer.print("Hello"); // Outputs: Hello
printer.print(100); // Outputs: 100
printer.print("Number", 200); // Outputs: Number 200
}
}
Method Overriding
Example in Java:
class Vehicle {
public void start() {
System.out.println("Vehicle is starting.");
}
}
// Usage
public class Main {
public static void main(String[] args) {
Vehicle myVehicle = new Car();
myVehicle.start(); // Outputs: Car is starting.
}
}
8. Interfaces and Abstract Classes
Interfaces
Example in Java:
public interface Drawable {
void draw();
}
// Usage
public class Main {
public static void main(String[] args) {
Drawable drawable = new Circle();
drawable.draw(); // Outputs: Drawing a circle.
}
}
Abstract Classes
● Cannot be instantiated.
● May contain abstract methods and concrete methods.
● A subclass must override all abstract methods unless it is also abstract.
Example in Java:
public abstract class Animal {
public abstract void makeSound();
// Usage
public class Main {
public static void main(String[] args) {
Animal animal = new Cat();
animal.makeSound(); // Outputs: Meow!
animal.sleep(); // Outputs: Sleeping...
}
}
9. Association, Aggregation, and Composition
Association
Example in Java:
class Teacher {
private String name;
// Constructor, getters, setters
}
class Student {
private String name;
// Constructor, getters, setters
}
class Course {
private Teacher teacher;
private List<Student> students;
Example in Java:
class Department {
private String name;
private List<Teacher> teachers;
Example in Java:
class Engine {
// Engine details
}
class Car {
private Engine engine;
public Car() {
created engine = new Engine(); // Engine is created when Car is
}
}
Singleton Pattern
● Ensures that a class has only one instance and provides a global point of access to it.
Example in Java:
public class Singleton {
private static Singleton instance;
private Singleton() {
// Private constructor prevents instantiation
}
// Usage
public class Main {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
}
}
Factory Pattern
● Defines an interface for creating an object but lets subclasses alter the type of objects
that will be created.
Example in Java:
interface Shape {
void draw();
}
class ShapeFactory {
public static Shape getShape(String shapeType) {
if ("CIRCLE".equalsIgnoreCase(shapeType)) {
return new Circle();
} else if ("RECTANGLE".equalsIgnoreCase(shapeType)) {
return new Rectangle();
}
return null;
}
}
// Usage
public class Main {
public static void main(String[] args) {
Shape shape1 = ShapeFactory.getShape("CIRCLE");
shape1.draw(); // Outputs: Drawing Circle.
Shape shape2 = ShapeFactory.getShape("RECTANGLE");
shape2.draw(); // Outputs: Drawing Rectangle.
}
}
O - Open/Closed Principle
Suppose we have a Worker interface that includes methods not all workers need:
interface Worker {
void work();
void eat();
}
This violates the Interface Segregation Principle because not all workers may need to eat()
(e.g., robots). A better approach:
interface Workable {
void work();
}
interface Eatable {
void eat();
}
@Override
public void eat() {
// Human eating
}
}
Example in Java:
public class Box<T> {
private T item;
public T get() {
return item;
}
}
// Usage
public class Main {
public static void main(String[] args) {
Box<String> stringBox = new Box<>();
stringBox.add("Hello");
System.out.println(stringBox.get()); // Outputs: Hello
Example in Java:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
} finally {
Annotations
● Provide metadata about the program that is not part of the program itself.
Example in Java:
java
Copy code
@Override
return "MyClass";
}
13. Interview Tips
● Understand Concepts Deeply: Go beyond memorizing definitions; understand how to
apply OOP concepts in real-world scenarios.
● Practice Coding: Write code by hand or on a whiteboard to simulate the interview
environment.
● Explain Your Thought Process: Be prepared to discuss why you chose a particular
solution.
● Review Java Syntax and Libraries: Be familiar with Java's core libraries and syntax.
● Study Common Design Patterns: Understand when and how to use patterns like
Singleton, Factory, Observer, etc.
● Prepare Examples: Have examples ready for each OOP concept.
● Ask Clarifying Questions: If a problem is not clear during the interview, ask for more
information.
● Stay Calm and Confident: Confidence shows that you are comfortable with the
material.
Final Thoughts:
Understanding OOP concepts thoroughly is essential for a software engineer. Using Java for
your examples provides concrete experience with a language widely used in the industry. By
mastering these concepts and practicing their application, you'll be well-prepared for your
technical interview.