SYSTEM ANALYSIS
NAME: JATINDER SINGH , BATCH: SEPTEMBER,
EMAIL:
[email protected] Total Marks 30
W
To
Ques 1. Explain the building blocks of an Object Oriented system along with the
suitable Examples.
ANS:Object-oriented systems are built on several fundamental concepts that allow
for the organization and manipulation of data and behavior. These building blocks
include:
Classes and Objects:
● Class: A blueprint or template for creating objects. It defines the properties
(attributes) and behaviors (methods) that all objects of that type will have.
● Object: An instance of a class. Objects are tangible entities that have state
(attributes) and behavior (methods) defined by their class.
Example:
python
class Car: def __init__(self, make, model): self.make = make self.model =
model def drive(self): print(f"{self.make} {self.model} is driving.") #
Creating objects of the Car class car1 = Car("Toyota", "Corolla") car2 =
Car("Honda", "Civic") car1.drive() # Output: Toyota Corolla is
driving.car2.drive() # Output: Honda Civic is driving.
Encapsulation:
● Encapsulation is the bundling of data and methods that operate on that data within
a single unit, such as a class. It hides the internal state of an object from the
outside world and only exposes the necessary functionality.
Example:
python
class BankAccount: def __init__(self, balance=0): self.balance = balance
defdeposit(self, amount): self.balance += amount def withdraw(self, amount):
ifself.balance >= amount: self.balance -= amount else: print("Insufficient
funds") # Encapsulating data and methods related to a bank account account =
BankAccount() account.deposit(1000) account.withdraw(500)
Inheritance:
● Inheritance allows a class (subclass) to inherit attributes and methods from
another class (superclass). It promotes code reusability and establishes a hierarchy
of classes.
Example:
python
class Animal: def speak(self): pass class Dog(Animal): def
speak(self):return "Woof!" class Cat(Animal): def speak(self): return
"Meow!" # Inheritance in action dog = Dog() cat = Cat() print(dog.speak())
# Output: Woof! print(cat.speak()) # Output: Meow!
Polymorphism:
● Polymorphism allows objects of different classes to be treated as objects of a
common superclass. It enables flexibility and extensibility in code by allowing
methods to behave differently based on the object they are called on.
Example:
python
# Using polymorphism to iterate over a list of animals animals = [Dog(),
Cat()] for animal in animals: print(animal.speak()) # Output: Woof! Meow!
These building blocks provide a robust framework for organizing and modeling complex systems
in an object-oriented manner.
Ques 2. Explain the concept of inheritance with a real-life example.
ANS:
Inheritance in object-oriented programming is a mechanism where a new class inherits
properties and behaviors (methods) from an existing class. It models the "is-a" relationship,
where a subclass is a specialized version of its superclass. Real-life examples of inheritance
abound, as it mimics the way objects and entities are related in the real world.
Let's consider a real-life example:
Vehicle Hierarchy
Suppose we have a hierarchy of vehicles, where each vehicle shares certain characteristics but
also has its own unique features.
Superclass - Vehicle: This represents the most general attributes and behaviors shared by
all vehicles. It might include attributes like speed, capacity, and methods like start, stop,
and accelerate.
Subclasses:
● Car: A car is a type of vehicle with additional attributes like the number of doors,
fuel type, and methods like honk.
● Truck: A truck is another type of vehicle with attributes like payload capacity, trailer
hitch, and methods like load, unload.
In this example, the superclass Vehicle provides a general blueprint for all types of vehicles,
while subclasses Car and Truck inherit from it and add their own specific features.
python
class Vehicle: def __init__(self, speed): self.speed = speed def
start(self):print("Vehicle started.") def stop(self): print("Vehicle
stopped.") classCar(Vehicle): def __init__(self, speed, num_doors):
super().__init__(speed) self.num_doors = num_doors def honk(self):
print("Beep! Beep!") classTruck(Vehicle): def __init__(self, speed,
payload_capacity):super().__init__(speed) self.payload_capacity =
payload_capacity defload(self): print("Truck loaded.") def
unload(self): print("Truck unloaded.")
Usage:
python
car = Car(100, 4) truck = Truck(80, 5000) car.start() # Output:
Vehicle started. truck.start() # Output: Vehicle started. car.honk()
# Output: Beep! Beep! truck.load() # Output: Truck loaded.
In this example, the Car and Truck classes inherit common attributes and behaviors from the
Vehicle superclass, such as speed, start(), and stop(), but they also have their own unique
attributes and methods (num_doors, honk() for Car, and payload_capacity, load(), unload() for
Truck). This hierarchical structure allows for code reuse, promotes modularity, and accurately
models real-world relationships between different types of vehicles.
3. List down the Advantages of using an Object-Oriented system.
ANS: Using an object-oriented system offers several advantages, including:
Modularity: Object-oriented systems promote modularity by organizing code into discrete,
self-contained objects. This makes it easier to understand, maintain, and modify code as
changes to one part of the system are less likely to affect other parts.
Reusability: Objects can be reused in different parts of an application or even in different
projects, reducing development time and effort. Inheritance allows for the creation of
subclasses that inherit properties and behaviors from parent classes, facilitating code
reuse.
Encapsulation: Encapsulation hides the internal state of objects and only exposes the
necessary functionality through methods. This protects the integrity of data and prevents
unintended modifications, leading to more robust and secure code.
Abstraction: Abstraction allows developers to focus on the essential details of an object
while hiding the irrelevant complexities. This simplifies the design and implementation
process and improves code readability and maintainability.
Polymorphism: Polymorphism allows objects of different classes to be treated
interchangeably through a common interface. This promotes flexibility and extensibility, as
new classes can be added without modifying existing code.
Easier Debugging and Maintenance: Object-oriented systems typically have a clear,
hierarchical structure, making it easier to isolate and debug issues. Additionally, code
changes are localized to specific classes or objects, reducing the risk of unintended side
effects.
Scalability: Object-oriented systems can scale effectively to handle larger and more
complex applications. New features can be added by creating new classes or extending
existing ones, without significantly impacting the overall architecture.
Code Organization: Object-oriented programming encourages a systematic and organized
approach to software development. Classes, objects, and their relationships provide a
clear and intuitive way to represent the structure and behavior of a system.
Facilitates Collaboration: Object-oriented systems allow developers to work on different
parts of a project concurrently, as long as they adhere to the defined interfaces and
contracts. This promotes collaboration and teamwork among developers.
Promotes Code Reusability and Maintainability: By encapsulating data and behavior
within objects and using inheritance and polymorphism, object-oriented systems promote
code reusability and maintainability. This leads to shorter development cycles and lower
maintenance costs over time.
Ques 4. What is the difference between a base class and a Derived
class? ANS: In object-oriented programming, particularly in languages that
support inheritance like Python, C++, or Java, there are two key types of classes:
base classes (also known as parent classes or superclasses) and derived classes
(also known as child classes or subclasses). The primary difference between
them lies in their relationship within the inheritance hierarchy:
Base Class (Parent Class, Superclass):
● A base class is the class from which other classes (derived classes) inherit
properties and behaviors.
● It typically represents a more general or abstract concept that is extended or
specialized by its subclasses.
● Base classes define common attributes and methods that are shared among
multiple related classes.
● Base classes are not dependent on any other class in the hierarchy.
● They are defined first and serve as the foundation upon which derived classes are
built.
Derived Class (Child Class, Subclass):
● A derived class is a class that inherits properties and behaviors from its base class.
● It extends or specializes the functionality of the base class by adding new attributes
or methods, or by overriding existing ones.
● Derived classes can have their own unique attributes and methods in addition to
those inherited from the base class.
● They are dependent on the base class and cannot exist without it.
● Derived classes are defined subsequent to their base classes and are created by
specifying the base class in their definition.
In summary, the key difference between a base class and a derived class lies in their roles within
the inheritance hierarchy: the base class serves as the foundation or blueprint, while the derived
class builds upon that foundation, inheriting and extending its properties and behaviors.