OBJECT ORIENTED PARADIGM (OOP)
Introduction:
The Object Oriented Paradigm (OOP) is a programming methodology that focuses on objects,
which are instances of classes. It models software using real-world entities, making programs
more modular, reusable, secure, and maintainable.
OOP overcomes the limitations of procedural programming by organizing data and methods
together.
Key Principles of OOP
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
======================================================================================
Introduction to Object
Definition:
An object is a real-world entity that combines:
State → variables (data members)
Behavior → methods (functions)
Real-World Example:
A Car object:
State: color, speed, fuel
Behavior: start(), accelerate(), stop()
Object and Class Relationship
Class → Blueprint or template
Object → Instance of a class
Java Example: Object Creation-
class Student
{
int rollNo;
String name;
void display()
{
[Link](rollNo + " " + name);
}
}
public class Main
{
public static void main(String[] args)
{
Student s1 = new Student(); // Object creation
[Link] = 101;
[Link] = "Amit";
[Link]();
}
}
Advantages of Objects
Represents real-world entities
Improves modularity
Enhances code readability
Enables reuse
======================================================================================
Object Diagram
Object diagrams are a visual representation in UML (Unified Modeling Language) that illustrates
the instances of classes and their relationships within a system at a specific point in time. They
display objects, their attributes, and the links between them, providing a snapshot of the system's
structure during execution.
An Object Diagram is a UML diagram that represents:
Objects
Their attribute values
Relationships between objects at a specific point in time (runtime snapshot).
Example:
Purpose of Object Diagram:
Understand object interactions
Visualize runtime behavior
Validate system design
Example Representation:
s1 : Student
-----------------
rollNo = 101
name = "Amit"
Java Example Related to Object Diagram-
class Book {
String title;
int price;
}
public class Main {
public static void main(String[] args) {
Book b1 = new Book();
[Link] = "Java Programming";
[Link] = 500;
}
}
For the above program, The object diagram will show:
Object name: b1
Class: Book
Attribute values (for title and price)
Difference: Class Diagram vs Object Diagram:
Class Diagram Object Diagram
Logical view Runtime view
No values Actual values
Blueprint Snapshot
======================================================================================
Information Hiding
Definition:
Information Hiding is the technique of restricting direct access to data and exposing only
required operations. It protects the internal state of an object.
Information Hiding can be Achieved in Java Using access modifiers:
private → accessible only within class
protected → accessible in same package or subclass
public → accessible everywhere
Java Example: Information Hiding
class BankAccount {
private double balance; // hidden data
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
[Link](5000);
[Link]([Link]());
}
}
Here:
balance cannot be accessed directly
Access is controlled via methods
Benefits of Information Hiding:
1. Improves security
2. Prevents misuse of data
3. Reduces dependency
4. Simplifies maintenance
======================================================================================
Abstract Data Type (ADT)
An Abstract Data Type (ADT) defines a data type by:
Operations allowed
Behavior of operations (Without specifying the internal implementation)
ADT focuses on what to do, not how to do.
Example: (Stack ADT)
Operations:
push()
pop()
peek()
isEmpty()
Java Implementation of Stack ADT
class Stack {
int arr[] = new int[5];
int top = -1;
void push(int x) {
arr[++top] = x;
}
int pop() {
return arr[top--];
}
}
public class Main {
public static void main(String[] args) {
Stack s = new Stack();
[Link](10);
[Link](20);
[Link]([Link]());
}
}
In the above program, User only uses operations, not internal logic.
======================================================================================
Inheritance
Inheritance allows a class to acquire properties and behaviors of another class, promoting code
reuse.
Inheritance is a fundamental concept in OOP(Object-Oriented Programming) in which one class is
allowed to inherit the features(variables and methods) of another class, i.e. it can reuse the
methods and fields of that class.
In Java “extends” keyword is used to links a subclass to a sup-erclass. Super-class is the "Parent"
(the original, general class), and a Subclass is the "Child" (the new, specialized class that inherits
from it).
Types of Inheritance in Java:
1. Single
2. Multilevel
3. Hierarchical
(Multiple inheritance using classes is not supported; achieved via interfaces)
Java Example: Inheritance
class Animal {
void eat() {
[Link]("Animal eats");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}
Advantages of Inheritance:
Code reuse
Reduced duplication
Easy maintenance
Logical hierarchy
======================================================================================
Polymorphism
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that
allows objects to behave differently based on their specific class type.
The word polymorphism means having many forms, and it comes from the Greek words poly
(many) and morph (forms), this means one entity can take many forms. In Java, polymorphism
allows the same method or object to behave differently based on the context.
In Java Polymorphism is mainly divided into two types:
1. Compile-Time Polymorphism (Method Overloading)
It is also known as static polymorphism and resolved by the compiler during the compilation
phase. It is achieved through method overloading.
Method overloading in Java means when there are multiple functions with the same name but
different parameters then these functions are said to be overloaded. Functions can be
overloaded by changes in the number of arguments or/and a change in the type of arguments.
public class Calculator {
public int add(int a, int b) { // Method 1: two integer parameters
return a + b;
}
public int add(int a, int b, int c) { // Method 2: three integer parameters
return a + b + c;
}
public double add(double a, double b) { // Method 3: two double parameters
return a + b;
}
}
Explanation: When add(10, 20) is called, the compiler selects Method1, When add(10, 20, 30) is
called, it selects Method2, and for add(10.1, 20.2), Method3 is chosen.
2. Run-Time Polymorphism (Method Overriding)
It is also known as Dynamic Polymorphism that is resolved at runtime. It is achieved
through method overriding, which requires an inheritance relationship between classes.
Method Overriding occurs when a subclass provides a specific implementation of a method that
is already defined in its super-class, with the exact same name, return type, and parameters.
Method overriding allows a subclass to modify or extend the behavior of an existing method in
the parent class.
class Animal {
void sound() {
[Link]("Animal sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
[Link]();
}
}
Output: Bark
Advantages of Polymorphism:
Flexibility
Code extensibility
Simplified interface
Dynamic method binding