Class Notes: Introduction to Object-Oriented
Programming (OOP) in Java
1. Introduction
Object-Oriented Programming (OOP) is a paradigm that organizes software design around data,
or objects, rather than functions and logic. Java is one of the most popular OOP languages.
2. Basic Concepts
- Class: A blueprint for creating objects.
- Object: An instance of a class.
- Method: Defines behavior of objects.
- Constructor: Special method to initialize objects.
3. Four Pillars of OOP
1. Encapsulation: Wrapping data and methods into a single unit (class).
2. Abstraction: Hiding internal details and showing only functionality.
3. Inheritance: Mechanism where one class acquires properties of another.
4. Polymorphism: One method, multiple forms (method overloading & overriding).
4. Encapsulation Example
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
5. Inheritance Example
class Animal {
void sound() { System.out.println("Animal makes a sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
}
public class TestInheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
6. Polymorphism Example
// Compile-time Polymorphism (Method Overloading)
class MathOperation {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
// Runtime Polymorphism (Method Overriding)
class Shape {
void draw() { System.out.println("Drawing shape"); }
}
class Circle extends Shape {
void draw() { System.out.println("Drawing circle"); }
}
7. Abstraction Example
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() { System.out.println("Car starts with key"); }
}
8. UML Basics
UML (Unified Modeling Language) is used to visually represent OOP concepts.
- Class Diagrams: Show classes and their relationships.
- Use Case Diagrams: Show system functionality.
- Sequence Diagrams: Show object interactions over time.
9. Advantages of OOP
- Modularity
- Code reusability
- Scalability
- Easier maintenance
10. Common Interview Questions
- Difference between abstraction and encapsulation?
- What is the difference between method overloading and overriding?
- Explain IS-A and HAS-A relationship.
- Can Java support multiple inheritance?
11. Summary
OOP in Java helps structure programs into modular, reusable, and scalable components.
The four pillars — Encapsulation, Abstraction, Inheritance, and Polymorphism — form the foundation.