OOPs by Piyush - Slashbyte
OOPs by Piyush - Slashbyte
SLASH BY TE
SSC-BA NK
NOTES
SLASHBYTE | SSC-BANK |
OOPs
What is a Class?
Definition:
A class is a blueprint or template for creating objects. It defines the properties
(attributes) and behaviors (methods) that the objects created from the class will
have. A class encapsulates data for the object and methods to manipulate that
data.
Structure of a Class:
A class typically consists of:
Diagram:
OOPs 1
classDiagram
class Car {
+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.
Diagram:
classDiagram
class Car {
+make
+model
+year
+start_engine()
}
car1: Car
car2: Car
OOPs 2
Access Modifier
Access modifiers (Public, Private, Protected) control access to classes,
methods, and variables, determining who can view, modify, or inherit them,
ensuring data encapsulation and security in object-oriented programming.
Or in a simpler format:
Access Outside
Class Package Subclass
Modifier Package
Public Y Y Y Y
Protected Y Y Y N
Private Y N N N
Key:
Y: Accessible
N: Not Accessible
Constructor vs Destructor
Characteristics Constructor Destructor
OOPs 3
Memory Allocation Functions in C
Function Purpose
Types:
Example:
Consider a
Car class that provides methods like start() , drive() , and stop() without
showing how the engine works.
Diagram:
OOPs 4
classDiagram
class Car {
+start()
+drive()
+stop()
}
2. Encapsulation
Definition:
Binding (or wrapping) code and data together into a single unit is known as
encapsulation.
Importance:
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.
OOPs 5
Encapsulation Implementation Protect data and behavior
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.
OOPs 6
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.
Types:
Example:
A method
draw() implemented in both Circle and Square classes.
Diagram:
classDiagram
class Shape {
+draw()
}
class Circle {
+draw()
OOPs 7
}
class Square {
+draw()
}
Shape <|-- Circle
Shape <|-- Square
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:
OOPs 8
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
+-------------------+
| 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.
OOPs 9
Diagram:
+---------------------+
| UserManager |
|---------------------|
| + createUser() |
| + logActivity() |
| + sessionTimeout() |
| + sendEmail() |
+---------------------+
8. Association
Definition:
Association defines a relationship between two classes. It represents how two
objects interact.
Types:
Example:
A
Teacher class associated with a School class.
Diagram:
classDiagram
class Teacher {
+teach()
}
OOPs 10
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
Libraryclass 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:
OOPs 11
classDiagram
class House {
+addRoom(room)
}
class Room {
+decorate()
}
House *-- Room
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)
}
Example:
A base class
Animal has a method makeSound() , and a derived class Dog overrides it.
Diagram:
OOPs 12
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()
}
C) A collection of functions
OOPs 13
A) Encapsulation
B) Inheritance
C) Polymorphism
D) Compilation
A) Better performance
C) Increased complexity
C) A type of destructor
D) A global function
OOPs 14
D) None of the above
A) Overloading
B) Inheritance
C) Encapsulation
D) Data hiding
A) implements
B) extends
C) inherits
D) uses
Answers
1. A) A blueprint for creating objects
2. D) Compilation
8. A) Overloading
OOPs 15
9. A) Defining multiple methods with the same name but different parameters
10. B) extends
OOPs 16