Oops Unit - 1
Oops Unit - 1
2
The Basic concepts of Object-Oriented Programming (OOP) in short:
2. Abstraction: Hiding complex implementation details and exposing only the essential
features of an object or system. It simplifies interaction by providing a clear interface
for the user.
3. Inheritance: A mechanism where a new class (subclass) inherits the properties and
behaviors (methods) of an existing class (superclass), promoting code reuse and
creating a hierarchical relationship.
4. Polymorphism: The ability of different classes to respond to the same method name
in different ways. This includes:
1. Modularity:
OOP breaks down a program into smaller, manageable sections called objects (instances
of classes). Each object is a self-contained module that can be developed, tested, and
maintained independently.
2. Code Reusability:
3
Through inheritance, new classes can reuse code from existing classes. This reduces
redundancy and promotes the reuse of code, making development faster and more
efficient.
3. Maintainability:
OOP makes it easier to update and modify code because of its modular structure.
Changes to one class or object generally do not affect others, which helps in maintaining
and debugging large systems.
4. Scalability:
The hierarchical structure (using inheritance) and the ability to extend classes make OOP
systems easier to scale, as new features can be added with minimal disruption to
existing code.
5. Security:
Encapsulation allows for data hiding, meaning that an object’s internal state is protected
from direct modification. This ensures more secure and stable programs.
6. Flexibility and Extensibility:
Polymorphism provides flexibility by allowing objects of different classes to be treated
uniformly. This enables developers to write more generic code that works with different
object types.
1. Identifying Objects: Understand the problem domain and identify key objects that
represent real-world entities (e.g., user, product, bank account).
2. Defining Classes and Objects: Group the objects into classes, which define their
attributes (data) and methods (behavior).
3. Establishing Relationships: Define how objects relate to one another (e.g.,
inheritance, association, composition).
4
4. Designing Interfaces: Specify the operations or methods that objects will use to
interact with each other.
The following are the typical steps involved in designing an object-oriented system:
5
3. Define Attributes and Methods
For each class, determine the properties (attributes) that the objects of the class will
have. These attributes represent the state of the object.
Define the methods (behaviors) that the objects will perform. These methods
manipulate the state or perform actions relevant to the object.
Establish how objects interact with each other. This step is crucial for defining how your
system’s components work together.
Consider relationships like association (one object uses another), inheritance (one class
extends another), and aggregation (one class is a part of another).
Plan the overall structure of the system. This includes how objects will communicate,
what classes will be responsible for which tasks, and how data flows throughout the
system.
Consider whether any design patterns (e.g., Singleton, Factory, Observer) can be applied
to solve common problems.
After designing the system, review the design to ensure that it meets all the
requirements and is efficient, scalable, and maintainable.
Refactor the design to eliminate redundancy, improve readability, and ensure that the
code can be easily modified in the future.
6
Translate the design into code using an object-oriented programming language.
Follow coding best practices, such as adhering to naming conventions, writing clean
code, and using comments where necessary.
Test individual components (unit testing) and the entire system (integration testing) to
ensure it functions as expected.
Perform validation and verification of the system against the initial requirements.
Design Examples :
1. Book
2. Member (Library User)
3. Library
4. Transaction
7
1. Book Class:
Attributes: title, author, ISBN, availabilityStatus
Methods:
checkAvailability(): Returns whether the book is available for borrowing.
borrowBook(): Marks the book as borrowed.
returnBook(): Marks the book as returned.
2. Member Class:
Methods:
borrowBook(book): Borrows a book from the library.
returnBook(book): Returns a borrowed book to the library.
3. Library Class:
Methods:
addBook(book): Adds a new book to the library collection.
registerMember(member): Registers a new member in the library.
listAvailableBooks(): Lists all books that are available for borrowing.
4. Transaction Class:
Methods:
8
createTransaction(book, member): Creates a borrowing transaction.
calculateFine(): Calculates any late fees for overdue books.
We refine the design to ensure scalability. For example, if the library expands, the
system can easily handle additional books or members without major changes. This can
be done by ensuring that the Library class can dynamically manage a growing list of
books and members.
Now, we would translate this design into an object-oriented programming language like
Java or Python. For instance, in Python, the Book class might look like this:
9
Step 8: Test the System
After coding, the system would be tested to ensure the functions like borrowing and
returning books work as expected, as well as handling edge cases like trying to borrow a
book that is already borrowed.
Conclusion
10
Comparison of Structured and Object-Oriented Programming Languages
1. Core Concept
Structured Programming:
Focuses on a linear flow of control, using sequences, decisions (if-else), loops (while,
for), and function calls.
Programs are divided into functions or procedures, where each function performs a
specific task, and the program flow is managed using a top-down approach.
The key idea is to organize the program into logical blocks of code for improved clarity
and reduced complexity.
Focuses on organizing code around objects that represent real-world entities. Objects
encapsulate data and behavior.
Programs are divided into classes (blueprints for objects), and objects are instances of
these classes.
OOP uses four main principles: Encapsulation, Inheritance, Polymorphism, and
Abstraction.
Structured Programming:
11
Programs are organized by functions or procedures, which are called to perform tasks. A
program is typically structured in a top-down fashion with clearly defined steps.
Data and functions are often separate, and data can be passed to functions as
arguments.
Object-Oriented Programming:
Programs are organized around objects and classes. A class is a blueprint, and an object
is an instance of that class.
Data and functions (methods) are bundled together within objects, ensuring that an
object is responsible for managing its state and behavior.
Structured Programming:
Object-Oriented Programming:
Data is encapsulated within objects, and access to this data is controlled through
methods. The object’s internal state is hidden from the outside world (data hiding).
Control is managed by interacting with objects through messages (method calls) rather
than direct manipulation of data.
4. Code Reusability
12
Structured Programming:
Object-Oriented Programming:
5. Modularity
Structured Programming:
Object-Oriented Programming:
OOP promotes strong modularity through the creation of classes. Each class is
responsible for its own data and behavior, and objects are loosely coupled.
Object interactions happen through well-defined interfaces (methods), which makes
large systems easier to manage and maintain.
13
6. Flexibility and Extensibility
Structured Programming:
Object-Oriented Programming:
OOP is highly extensible and flexible. Using inheritance, existing classes can be extended
with new functionality, and new classes can be created without modifying existing code.
Polymorphism allows the same method to be applied to different objects, enabling
flexible and reusable code.
Structured Programming:
Maintenance can be more challenging for large programs due to the complexity of
managing data and functions, especially when there are many interdependencies.
Debugging can be difficult, as global variables may be accessed or modified by any
function, leading to potential errors.
Object-Oriented Programming:
14
Encapsulation ensures that objects hide their internal state, making it easier to debug
and maintain the program without worrying about how data is shared or modified
across the system.
8. Example Languages
C: One of the most popular structured programming languages, where code is organized
into functions, and the flow is controlled using conditionals and loops.
Pascal: Another structured programming language, with a focus on clear and structured
code.
FORTRAN: A procedural programming language used for numeric and scientific
computing.
BASIC: Simple, structured language often used for beginner-level programming.
Python: An object-oriented language that also supports other paradigms and is popular
for rapid development.
Ruby: A pure object-oriented language where everything is an object.
9. Performance
Structured Programming:
15
Typically faster due to its simpler approach, as structured programs can be optimized
more easily with fewer layers of abstraction.
Performance depends heavily on the design and implementation, as large functions can
be inefficient.
Object-Oriented Programming:
Can be slower than structured programming due to the overhead of object creation,
method calls, and memory management.
However, modern OOP languages (like Java and Python) have optimized their runtime
environments, reducing performance differences.
Structured Programming:
Best suited for smaller or less complex systems where the program flow is relatively
simple, and data manipulation does not require extensive interaction across different
parts of the system.
Object-Oriented Programming:
Well-suited for large, complex systems, particularly when the system is likely to evolve
or require frequent maintenance.
OOP is ideal for applications with complex data structures or those that require
extensive user interaction and modular development.
Conclusion
16
Object-oriented programming provides a more powerful, modular approach suited for
larger, more complex systems. It encourages code reuse, easy maintenance, and
scalability by organizing the code around objects that model real-world entities.
Both paradigms have their strengths and can be chosen depending on the size,
complexity, and requirements of the software being developed. In modern software
development, many languages and applications combine features of both paradigms to
offer flexibility and efficiency.
17