0% found this document useful (0 votes)
70 views17 pages

OOPs by Piyush - Slashbyte

Uploaded by

ashwani5667
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views17 pages

OOPs by Piyush - Slashbyte

Uploaded by

ashwani5667
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

COMPLETE NOTES

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:

Attributes (Fields/Properties): Variables that hold data related to the class.

Methods (Functions/Procedures): Functions defined within the class that


operate on the attributes.

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.

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

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.

Access Modifier Accessibility Scope Inheritance

Public Everywhere Global Yes

Private Same class only Class-level No

Protected Same class, subclasses Class-level Yes

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

Static vs Instance vs Global


Variable Type Scope Lifetime Sharing

Static (Class) Class-level Program lifetime Shared among instances

Instance Instance-level Instance lifetime Unique to each instance

Global Program-level Program lifetime Accessible from anywhere

Constructor vs Destructor
Characteristics Constructor Destructor

Purpose Initialize object Clean up resources

Calling When object created When object destroyed

Name Same as class name Prefix '~' to class name

Parameters Can have parameters No parameters

OOPs 3
Memory Allocation Functions in C
Function Purpose

malloc() Allocate block of memory

calloc() Allocate and initialize memory to zero

realloc() Resize allocated memory block

free() Deallocate memory

Dynamic Memory Allocation in C++


Operator Purpose

new Allocate memory

delete Deallocate memory

new[] Allocate array memory

delete[] Deallocate array memory

Object-Oriented Programming (OOP)


Concepts in Detail
1. Abstraction
Definition:
Abstraction is the process of simplifying complex systems by modeling classes
based on the essential properties and behaviors of that system, while hiding
the irrelevant details.

Types:

Data Abstraction: Hiding the details of data representation.

Control Abstraction: Hiding the implementation details of methods.

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:

Protects object integrity by preventing external interference.

Facilitates code maintenance.

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.

Concept Focus Purpose

Data Abstraction Interface Simplify complex systems

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:

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()

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:

Tight Coupling: Classes are highly dependent on one another.

Loose Coupling: Classes are independent and communicate through


interfaces.

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

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.

OOPs 9
Diagram:

+---------------------+
| 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()
}

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

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:

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()
}

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?

OOPs 13
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

OOPs 14
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

7. A) Hides the complexity of the implementation details

8. A) Overloading

OOPs 15
9. A) Defining multiple methods with the same name but different parameters

10. B) extends

Variable Type Scope Lifetime Sharing

Static (Class) Class-level Program lifetime Shared among instances

Instance Instance-level Instance lifetime Unique to each instance

Global Program-level Program lifetime Accessible from anywhere

OOPs 16

You might also like