Oops
Oops
What is a Class?
A class is a blueprint or template for creating objects in object-oriented programming (OOP). It defines the attributes (data
members/fields) and methods (functions/behaviors) that the objects instantiated from the class will have.
A class provides encapsulation, meaning it groups the data and functions that operate on that data together. It also promotes code
reuse and modularity.
Structure of a Class
A class typically consists of:
1. Attributes (Fields/Properties) → These are variables that store the state or characteristics of an object.
2. Methods (Functions/Procedures) → These are functions defined within the class that operate on the attributes and define the
behavior of the object.
3. Constructor → A special method used to initialize objects when they are created.
4. Access Modifiers → Keywords like public, private, and protected that control access to attributes and methods.
class example
classDiagram
class Car {
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 1/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
+make
+model
+year
+start_engine()
}
What is an Object?
Definition:
An object is an instance of a class. It is a concrete implementation of the class blueprint, containing specific data (state) and the methods
defined by the class. Objects are the fundamental building blocks of OOP.
Characteristics of Objects:
1. State: Represented by the attributes of the class. For example, the make, model, and year attributes of the Car class represent the state
of a specific car object.
2. Behavior: Represented by methods that can operate on the object's state. For example, calling the start_engine() method modifies or
utilizes the state of the object.
Diagram:
classDiagram
class Car {
+make
+model
+year
+start_engine()
}
car1: Car
car2: Car
Access Modifier
Access Modifiers in Java (Theory)
Access modifiers in Java define the visibility and accessibility of classes, methods, and variables. They help enforce encapsulation and
security in object-oriented programming.
private ✅ Yes ❌ No ❌ No ❌ No
default (no ✅ Yes ✅ Yes ❌ No ❌ No
keyword)
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 2/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
• Used when a class and its members should be shared only within the package.
Key Takeaways
✔ private → Most restrictive, used for data hiding.
✔ default → Package-private, allows access only within the same package.
✔ protected → Accessible within the same package + subclasses in different packages.
✔ public → Least restrictive, accessible everywhere.
Using proper access modifiers ensures data security, encapsulation, and better code organization. 🚀
Comparison Table
Variable Type Scope Lifetime Sharing
(Where it is accessible) (How long (Access across
it exists) objects/classes)
Static (Class Within the class (shared Exists for Shared among
Variable) among all instances) the entire all instances of
program the class
execution
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 3/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
2. Instance Variables
• Each object (instance) gets its own copy of the variable.
• Memory is allocated when an object is created and deallocated when the object is destroyed.
• Changes made to one object’s instance variable do not affect other objects.
• Useful for storing unique attributes of an object.
Key Points:
✔ Defined inside the class but outside methods.
✔ Lifetime is until the object is garbage collected.
✔ Used for object-specific data (e.g., a person’s name, car model).
3. Global Variables
• Declared outside of all classes and functions, making them accessible throughout the program.
• Exists from the start to the end of the program.
• Can be accessed and modified from anywhere, making them less secure.
• Generally avoided in OOP due to poor encapsulation and risk of unintended modifications.
Key Points:
✔ Accessible from any class or function.
✔ Exists throughout the entire program execution.
✔ Used in small scripts but discouraged in large applications.
Best Practices
✅ Use static variables for constants and shared resources.
✅ Use instance variables for object-specific data.
✅ Avoid global variables in large applications due to poor security and maintainability.
PYQS
1. What is the primary purpose of a class in Object-Oriented Programming? (IBPS SO 2023)
A) To store multiple values in a single variable
B) To define a blueprint for creating objects
C) To execute a block of code repeatedly
D) To allow direct access to private members
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 4/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
2. Which access modifier allows a variable to be accessed only within the same class? (IBPS SO 2022)
A) public
B) protected
C) private
D) default
✅ Correct Answer: C) private
📌 Explanation: The private access modifier restricts access to within the same class only. It is used to hide data from external access.
3. What is the key difference between a static variable and an instance variable? (IBPS SO 2023)
A) Static variables are stored per object, whereas instance variables are shared among all objects
B) Static variables are shared among all instances, while instance variables are unique to each object
C) Instance variables can be accessed without creating an object, whereas static variables require an instance
D) There is no difference; both work the same way
✅ Correct Answer: B) Static variables are shared among all instances, while instance variables are unique to each object
📌 Explanation: A static variable belongs to the class and is shared across all instances, whereas an instance variable is specific to each
individual object.
4. In Java, which access modifier allows visibility within the same package but not outside it? (IBPS SO 2021)
A) public
B) private
C) protected
D) default (no modifier)
✅ Correct Answer: D) default (no modifier)
📌 Explanation: If no access modifier is specified, the default (package-private) modifier is applied, making the member accessible only
within the same package but not outside it.
5. What is the key disadvantage of using global variables in Object-Oriented Programming? (IBPS SO 2021)
A) They take up more memory
B) They cannot be modified once declared
C) They violate encapsulation and can lead to unintended modifications
D) They make code more efficient
✅ Correct Answer: C) They violate encapsulation and can lead to unintended modifications
📌 Explanation: Global variables can be accessed from anywhere in the program, making them prone to unintended changes and
violating data encapsulation, which is a core OOP principle.
Call Mechanism Automatically called when an object is Automatically called when an object
instantiated goes out of scope or is manually
deleted
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 5/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
Declaration Uses the same name as the class Uses a tilde (~) before the class
name
Access Can be public, private, or protected Usually public but rarely defined
Modifiers explicitly
Explicit Call Can be called explicitly (though not Cannot be called explicitly, only
common) invoked by the system
Use Case Used for setting default values, Used for releasing memory, closing
memory allocation, and object setup file handles, or cleaning up resources
Types of Constructors
1. Default Constructor (No-Argument Constructor)
• A constructor that takes no parameters and initializes objects with default values.
• If no constructor is defined, the compiler provides a default constructor.
2. Parameterized Constructor
• A constructor that takes arguments to initialize an object with specific values.
• Allows different objects to have different initial values.
3. Copy Constructor
• A constructor that creates a new object by copying an existing object.
• Used in object cloning.
4. Static Constructor (C# only)
• Used to initialize static members of a class.
• Runs only once when the class is loaded into memory.
5. Private Constructor
• Used to restrict object creation outside the class.
• Useful in singleton design patterns.
Key Takeaways
✔ Constructors initialize objects, while destructors clean up resources.
✔ Constructors can be overloaded, but destructors cannot.
✔ Destructors are automatically invoked when an object is destroyed.
✔ Different types of constructors allow flexibility in object initialization.
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 6/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
• Returns a void pointer (void*), which needs to be typecast into the required type (int*, float*, etc.).
• If allocation fails, it returns NULL.
Key Features:
✔ Allocates uninitialized memory.
✔ Returns a void pointer that must be typecast.
✔ If memory allocation fails, it returns NULL.
3. realloc() (Reallocation)
Definition:
realloc() is used to resize previously allocated memory without losing existing data.
Syntax:
void* realloc(void* ptr, size_t new_size);
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 7/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
Types of Abstraction
1. Data Abstraction:
2. Hides how data is stored and manipulated. It focuses on what the data represents rather than its internal structure.
• Example: A BankAccount class hides how the balance is stored and only exposes methods like deposit or withdraw.
3. Control Abstraction:
4. Hides the implementation details of operations. It focuses on what the operation does, without revealing the internal logic.
• Example: A method like deposit() hides how the transaction is processed and simply provides a way to interact with the
account.
Example:
Consider a
Car class that provides methods like start(), drive(), and stop() without showing how the engine works.
Diagram:
classDiagram
class Car {
+start()
+drive()
+stop()
}
2. Encapsulation in OOP
Definition:
• Encapsulation is the concept of bundling the data (attributes) and the methods (functions) that operate on the data into a single unit,
called a class. It restricts direct access to some of an object's components and only exposes necessary parts through public methods.
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 8/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
This helps in protecting the object's state from unauthorized access and modification.
• Importance of Encapsulation:
1. Protects Object Integrity:
2. It ensures that an object's data is protected and can only be accessed or modified in controlled ways through methods (getters and
setters).
3. Prevents External Interference:
4. Encapsulation hides implementation details, preventing accidental or harmful changes from outside the object.
5. Facilitates Code Maintenance:
6. Changes to the internal workings of an object can be made without affecting other parts of the program, making the code easier to
maintain.
• Key Concepts:
• Private Attributes: Internal data is hidden using access modifiers (like private) to prevent external manipulation.
• Public Methods: Provide controlled access to the data (e.g., getBalance(), setBalance()).
• Data Hiding: Internal details are hidden, exposing only what is necessary for interaction.
• Encapsulation improves security, simplifies code, and enhances maintainability by ensuring controlled access to an object's data.
•
Example of Encapsulation in a Business Context
Consider a company with various divisions: Sales, Finance, and Accounts.
1. Finance Division: Manages all financial transactions and maintains financial records.
2. Sales Division: Handles all sales-related tasks and keeps track of each sale.
Now, if a finance officer needs sales information for a specific month, they cannot directly access the sales data. Instead, they must interact with
the Sales Division, which encapsulates the sales information and restricts access from other divisions.
This encapsulation ensures that:
• Only authorized personnel can access and modify the data.
• Each division operates independently while still being able to share necessary information through appropriate channels.
3. Inheritance
Definition:
Inheritance is a mechanism where one class inherits properties and methods from another class. It promotes code reusability.
Types:
Example:
A Dog class inherits from an Animal class.
Diagram:
classDiagram
class Animal {
+eat()
}
class Dog {
+bark()
}
Animal <|-- Dog
4. Polymorphism
Definition:
Polymorphism is the ability of different classes to be treated as instances of the same class through a common interface. It allows one interface
to be used for a general class of actions.
If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something,
for example, shape, triangle, rectangle, etc.
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 9/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
Types:
• Compile-time Polymorphism (Method Overloading): The method is related to the same name with different signatures.
• Runtime Polymorphism (Method Overriding): A method in a derived class overrides a method in the base class.
Example:
A method
draw() implemented in both Circle and Square classes.
Diagram:
classDiagram
class Shape {
+draw()
}
class Circle {
+draw()
}
class Square {
+draw()
}
Shape <|-- Circle
Shape <|-- Square
PYQS
1. What is the primary goal of Abstraction in Object-Oriented Programming?
(SBI SO)
a) To simplify complex systems by hiding unnecessary details
b) To increase the security of the system
c) To create reusable code
d) To decrease the runtime of the application
Answer: a) To simplify complex systems by hiding unnecessary details
Explanation: Abstraction simplifies complex systems by focusing on what the object does rather than how it works internally.
3. Which concept allows access control in OOP by hiding internal details of an object?
(SBI SO)
a) Inheritance
b) Polymorphism
c) Encapsulation
d) Abstraction
Answer: c) Encapsulation
Explanation: Encapsulation involves wrapping the data and methods into a single unit and restricting access to internal details via access
modifiers.
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 10/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
5. What type of inheritance allows one class to inherit properties from multiple classes?
(SBI SO)
a) Single Inheritance
b) Multiple Inheritance
c) Multi-level Inheritance
d) Hierarchical Inheritance
Answer: b) Multiple Inheritance
Explanation: Multiple Inheritance allows a class to inherit properties from more than one base class, although it’s restricted in some languages
like Java (can be achieved using interfaces).
6. In which OOP concept is a subclass able to reuse methods and properties of a superclass?
(IBPS SO)
a) Abstraction
b) Polymorphism
c) Inheritance
d) Encapsulation
Answer: c) Inheritance
Explanation: Inheritance allows a subclass to inherit properties and methods from a superclass, promoting code reuse.
8. Which concept hides unnecessary details from the user while exposing only essential features?
(IBPS SO 2024)
a) Abstraction
b) Encapsulation
c) Inheritance
d) Polymorphism
Answer: a) Abstraction
Explanation: Abstraction hides the complexities of a system, allowing users to interact with it at a high level, focusing only on essential
functions.
9. What type of OOP relationship is illustrated when a Car class inherits properties like speed and fuel from a Vehicle class?
a) Inheritance
b) Abstraction
c) Encapsulation
d) Polymorphism
Answer: a) Inheritance
Explanation: Inheritance allows a Car class to inherit common properties and methods from a Vehicle class, promoting code reuse.
10. Which OOP principle allows a class to be reused by inheriting methods and attributes from another class without modifying the
original class?
(IBPS SO)
a) Encapsulation
b) Inheritance
c) Abstraction
d) Polymorphism
Answer: b) Inheritance
Explanation: Inheritance allows one class (child) to inherit properties and methods from another class (parent), enabling code reuse without
modifying the original class.
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 11/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
5. Message Passing
Definition:
Message passing is how objects communicate with each other. It involves sending and receiving messages to invoke the behavior of another
object.
Example:
In a chat application, a
User class can send messages to another User.
Diagram:
classDiagram
class User {
+sendMessage(message)
+receiveMessage()
}
class ChatApp {
+startChat(user1, user2)
}
6. Coupling
Definition:
Coupling refers to the degree of direct knowledge a class has about another class. Lower coupling is preferred as it enhances modularity.
Types:
• Tight Coupling: Classes are highly dependent on one another.
• Loose Coupling: Classes are independent and communicate through interfaces.
Example:
Using interfaces and abstract classes to achieve loose coupling.
7. Cohesion
Definition:
Cohesion refers to how closely related and focused the responsibilities of a single class are. High cohesion is desirable, as it makes the class
easier to understand and maintain.
Types:
High Cohesion
Definition: High cohesion occurs when the components of a module or class are closely related and focused on a single responsibility, making
the system easier to maintain, understand, and reuse.
Diagram:
+-------------------+
| Order Class |
|-------------------|
| + placeOrder() |
| + cancelOrder() |
| + checkStatus() |
+-------------------+
|
v
+-------------------+
| Order Details |
|-------------------|
| - orderID |
| - orderDate |
| - customerDetails |
+-------------------+
Low Cohesion
Definition: Low cohesion occurs when the components of a module or class are unrelated and perform multiple, distinct tasks, leading to a
complex and hard-to-maintain system.
Diagram:
+---------------------+
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 12/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
| UserManager |
|---------------------|
| + createUser() |
| + logActivity() |
| + sessionTimeout() |
| + sendEmail() |
+---------------------+
• High Cohesion: Focused on one responsibility, leading to better readability and maintenance.
• Low Cohesion: Performs unrelated tasks, resulting in complexity and decreased maintainability.
8. Association
Definition:
Association defines a relationship between two classes. It represents how two objects interact.
Types:
• One-to-One: One object of class A is associated with one object of class B.
• One-to-Many: One object of class A is associated with multiple objects of class B.
• Many-to-Many: Multiple objects of one class are associated with multiple objects of another class.
Example:
A
Teacher class associated with a School class.
Diagram:
classDiagram
class Teacher {
+teach()
}
class School {
+admitStudent()
}
Teacher --> School
9. Aggregation
Definition:
Aggregation is a special form of association that represents a "whole-part" relationship, where parts can exist independently of the whole.
Example:
A
Library class that contains Books, which can be borrowed or exist independently.
Diagram:
classDiagram
class Library {
+addBook(book)
}
class Book {
+read()
}
Library o-- Book
10. Composition
Definition:
Composition is a stronger form of aggregation. It implies ownership and a "part-of" relationship, where the lifecycle of the part is tied to the
lifecycle of the whole.
Example:
A
House class composed of Rooms, which cannot exist without the house.
Diagram:
classDiagram
class House {
+addRoom(room)
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 13/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
}
class Room {
+decorate()
}
House *-- Room
11. Method Overloading
Definition:
Method overloading allows a class to have multiple methods with the same name but different parameters. It supports compile-time
polymorphism.
Example:
A class
MathOperations with overloaded methods for add.
Diagram:
classDiagram
class MathOperations {
+add(int a, int b)
+add(double a, double b)
+add(int a, int b, int c)
}
12. Method Overriding
Definition:
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
Example:
A base class
Animal has a method makeSound(), and a derived class Dog overrides it.
Diagram:
classDiagram
class Animal {
+makeSound()
}
class Dog {
+makeSound()
}
Animal <|-- Dog
13. Exceptions
Definition:
Exceptions are anomalies that occur during the execution of a program. Exception handling is a mechanism to respond to exceptions and
ensure the program continues running or gracefully terminates.
Example:
Catching and handling an
ArithmeticException in case of division by zero.
Diagram:
classDiagram
class Exception {
+catch()
+throw()
}
OOP Multiple-Choice Questions
1. What is a class in OOP?
• A) A blueprint for creating objects
• B) A specific object created from a class
• C) A collection of functions
• D) A data type that represents a single value
2. Which of the following is NOT a principle of OOP?
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 14/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
• A) Encapsulation
• B) Inheritance
• C) Polymorphism
• D) Compilation
3. What is inheritance in OOP?
• A) A mechanism where one class acquires properties from another
• B) An object's ability to take on multiple forms
• C) Protecting the internal state of an object
• D) A way to organize code into different categories
4. Which of these is an example of polymorphism?
• A) A single function with multiple implementations
• B) A class with multiple constructors
• C) A variable that can hold different data types
• D) An object that interacts with another object
5. What does encapsulation achieve?
• A) Better performance
• B) Protection of object state
• C) Increased complexity
• D) Direct access to data
6. What is a constructor in OOP?
• A) A method that performs calculations
• B) A special method for initializing objects
• C) A type of destructor
• D) A global function
7. What does data abstraction do?
• A) Hides the complexity of the implementation details
• B) Exposes all details of data
• C) Combines multiple classes
• D) None of the above
8. Which of the following allows a function to operate on different types of data?
• A) Overloading
• B) Inheritance
• C) Encapsulation
• D) Data hiding
9. What is method overloading?
• A) Defining multiple methods with the same name but different parameters
• B) Creating a method that calls itself
• C) Defining a method in a subclass that has the same name as its superclass
• D) None of the above
10. Which keyword is used to inherit a class in most OOP languages?
• A) implements
• B) extends
• C) inherits
• D) uses
Answers
1. A) A blueprint for creating objects
2. D) Compilation
3. A) A mechanism where one class acquires properties from another
4. A) A single function with multiple implementations
5. B) Protection of object state
6. B) A special method for initializing objects
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 15/16
2/17/25, 1:12 PM OOPs | Detailed Syllabus
https://app.clickup.com/9016631242/v/dc/8cpxqya-896/8cpxqya-836 16/16