OOP-CG UNIT 1 NOTES
OOP-CG UNIT 1 NOTES
site
• The Big Idea: This is one of the earliest and most straightforward paradigms.
You write a program as a series of sequential, step-by-step instructions, like a
recipe or a to-do list. The focus is on procedures (also known as functions or
subroutines) that perform specific tasks on data. Data and the procedures that
operate on it are kept separate.
• Analogy: You're building a bookshelf. The procedural approach is a list of
instructions: "1. Get wood. 2. Cut wood to size. 3. Get screws. 4. Screw pieces
together. 5. Paint the shelf." The data (wood, screws) and the actions (cut, screw,
paint) are separate concepts.
• Characteristics:
o Top-down design.
o Code is organized into functions.
o Data is often stored in global variables, accessible to all functions.
o Focuses on the "how" – the sequence of actions.
• Example Languages: C, Pascal, FORTRAN.
• The Big Idea: Instead of a to-do list, OOP views a program as a collection of
interacting objects. An object is a self-contained unit that bundles together
both data (attributes) and the behaviors (methods) that operate on that data.
This is the cornerstone of OOP.
• Analogy: Instead of a list of instructions for building a car, you think about the
car itself as an object. A Car object has data (color, model, current speed) and
behaviors (startEngine(), accelerate(), brake()). The internal complexity of the
engine is hidden; you just interact with the simple interface (pedals, steering
wheel).
• Characteristics:
o Bottom-up design.
o Focuses on "what" things are, not just "how" to do things.
o Data and behavior are tightly coupled within objects.
o Emphasizes concepts like Encapsulation, Inheritance, and Polymorphism.
• Example Languages: Java, C++, Python, C#.
principle is that a function, given the same input, will always produce the same
output, with no side effects (like modifying a global variable or printing to the
screen).
• Analogy: It's like a pure mathematical machine. If you put 2 and 3 into
an add machine, it will always spit out 5. It will never change a number written
on a whiteboard outside the machine. This "no side effects" rule is crucial.
• Characteristics:
o Focus on immutable data (data that cannot be changed after creation).
o Functions are "first-class citizens" (can be passed around like variables).
o Avoids loops in favor of recursion and higher-order functions
(like map, filter, reduce).
• Example Languages: Haskell, Lisp, F#, JavaScript (supports functional
concepts).
• The Big Idea: This paradigm is completely different. Instead of telling the
computer how to solve a problem, you tell it what you know (a set of facts and
rules) and then ask it a question. The computer uses logical deduction to figure
out the answer.
• Analogy: You are a detective. You have a list of facts ("Socrates is a man.") and
rules ("All men are mortal."). You then ask a question ("Is Socrates mortal?").
The logic engine connects the dots and answers "Yes." You didn't tell it the steps
to figure it out; you just gave it the knowledge base.
• Characteristics:
o Declarative style (you declare what is true, not how to compute).
o Based on formal logic.
o Consists of a database of facts and rules.
• Example Language: Prolog.
As programs grew larger and more complex, the procedural paradigm started to show
its weaknesses. Global data could be modified by any function anywhere in the
program, leading to unpredictable bugs that were hard to trace ("spaghetti code"). It
was difficult to model real-world problems and reuse code effectively.
• Managing Complexity: By bundling data and behavior into objects, OOP creates
self-contained, manageable units.
• Real-World Modeling: It's more natural to think about problems in terms of
real-world objects (a Student, an Invoice, a Button) rather than abstract
procedures.
• Code Reusability: Inheritance allows us to create new classes that reuse the
properties and methods of existing classes.
• Maintainability: Encapsulation protects data from accidental modification,
making code safer and easier to maintain and debug.
These are the fundamental concepts that define the object-oriented paradigm.
• Class: The blueprint or template for creating objects. A class defines the
common attributes (data members) and behaviors (methods) that all objects of
its type will have.
String color;
int speed;
void drive() {
System.out.println("Car is driving...");
myCar.color = "Red";
• Data Members (Attributes/Fields): The variables inside a class that hold the
state or properties of an object. Data members are variables defined inside a
class. They represent the state of an object.
String name;
int age;
• Methods (Behaviors): The functions defined inside a class that perform actions
or operate on the object's data. Methods are functions defined inside a class.
They represent the behavior of an object.
void study() {
System.out.println("Student is studying...");
• Data Encapsulation: The bundling of data (attributes) and the methods that
operate on that data into a single unit (the class). This is the foundational
concept of OOP.
balance += amount;
return balance;
• Information Hiding & Data Abstraction: These two concepts work together,
enabled by encapsulation.
o Information Hiding: The practice of hiding the internal complexity of an
object from the outside world. This is achieved by making data
members private and providing controlled access
through public methods (getters and setters). This protects the object's
internal state.
password = pwd;
}}
• Inheritance: The mechanism by which one class (the child or subclass) can
acquire the properties and methods of another class
(the parent or superclass). This supports the "is-a" relationship.
class Animal {
void makeSound() {
System.out.println("Animal sound");
void bark() {
System.out.println("Dog barks");
class Animal {
void makeSound() {
System.out.println("Animal sound");
@Override
void makeSound() {
System.out.println("Meow");
Benefits of OOP
• Why Java is Strongly OOP: In Java, virtually everything is an object, and all code
must reside within a class. You cannot have a standalone function like in C++.
• Platform Independence (The JVM):
o JVM (Java Virtual Machine): The JVM is the magic that makes Java
platform-independent ("Write Once, Run Anywhere").
o Process:
1. You write Java source code (.java file).
2. The Java compiler compiles it into an intermediate, platform-neutral
format called bytecode (.class file).
3. The JVM, which is a program designed for a specific operating
system (Windows, macOS, Linux), takes this bytecode and translates
it into native machine code that the host computer can execute.
o Textual Diagram:
[YourCode.java] --(Compiler)--> [YourCode.class (Bytecode)] --(JVM)-->
[Native Machine Code for Windows/Mac/Linux]
// Class definition
System.out.println("Hello, World!");
In Java, when you declare a variable, you are essentially creating a named box in the
computer's memory. What you can put inside that box is determined by its data type.
There are two fundamentally different kinds of boxes.
1. Primitive Types:
Primitive types are the most basic, fundamental building blocks for data. They are
simple and efficient. When you declare a primitive variable, the box in memory holds
the actual value itself.
2. Reference Types:
Reference types are used for more complex data structures and objects. When you
declare a reference variable, the box in memory does not hold the object itself.
Instead, it holds a memory address—a "reference" or "pointer"—that tells the
computer where the actual, larger object is stored in a different part of memory called
the heap.
• Classes: String, Scanner, or any class you create yourself (Car, Student, etc.).
• Arrays: int[], String[], etc.
int x = 10;
System.out.println(y); // Output: 20
System.out.println(arr2[0]); // Output: 99
The Java Class Libraries (also known as the Java API) are a massive collection of pre-
written, ready-to-use code that comes with Java. It's like having a giant, professionally
organized toolkit. Instead of building everything from scratch, you can just grab a tool
that does what you need.
• Organized into Packages: These tools are organized into "packages" (like
drawers in the toolkit). To use a tool from a package, you must first import it.
• Example: If you want to read input from the user, you don't need to write
complex code to handle the keyboard. You just import the Scanner class from
the java.util package.
import java.util.Scanner; // Importing the Scanner tool from the 'util' drawer.
Control statements are the "traffic signals" of your program. They let you make
decisions and repeat actions, turning a simple list of instructions into a smart, dynamic
program.
• if Statement
o Purpose: Executes a block of code only if a certain condition is true.
o Syntax:
if (condition) {
o Example:
• if-else Statement
o Purpose: Provides an alternative path. It executes one block of code if the
condition is true, and a different block if it is false.
o Syntax:
if (condition) {
} else {
o Example:
System.out.println("You passed!");
} else {
if (condition1) {
} else if (condition2) {
} else {
o Example:
System.out.println("Excellent (A)");
System.out.println("Good (B)");
System.out.println("Average (C)");
} else {
System.out.println("Needs Improvement");
• for loop
o Purpose: The perfect loop for when you know exactly how many times
you want to repeat an action.
o Syntax: for (initialization; condition; update) { ... }
o Example: Print numbers from 1 to 5.
System.out.println(i);
• while loop
o Purpose: A flexible loop that repeats as long as a condition is true. The
condition is checked before each iteration.
o Syntax: while (condition) { ... }
o Example: A simple countdown.
int countdown = 3;
System.out.println(countdown);
System.out.println("Blast off!");
• do-while loop
o Purpose: Similar to a while loop, but it guarantees that the loop body will
be executed at least once. The condition is checked after each iteration.
o Syntax: do { ... } while (condition);
o Example: A menu that must be shown at least once.
int choice;
do {
System.out.println("0. Exit");
choice = input.nextInt();
3. Jump Statements
• break
o Purpose: To immediately exit the current loop (or switch statement).
o Example: Stop a loop as soon as a target number is found.
if (num == 30) {
System.out.println("Found 30!");
• continue
o Purpose: To skip the rest of the current iteration and jump straight to the
beginning of the next iteration.
o Example: Print only the odd numbers in a range.
System.out.println(i);
Conclusion: Java enforces a pure OOP structure with a focus on safety and portability
(via the JVM and garbage collection). C++ offers more low-level control and
performance at the cost of complexity and manual memory management. Python
provides maximum flexibility and rapid development speed with its dynamic typing
and multi-paradigm approach.