0% found this document useful (0 votes)
16 views3 pages

Polymorphism Activity

Uploaded by

Lawrence Novero
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)
16 views3 pages

Polymorphism Activity

Uploaded by

Lawrence Novero
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/ 3

Name: NOVERO, MARK LAWRENCE A.

CPE201

I. Multiple choice
1. Which of the following best describes abstraction in object-oriented programming?
a. Encapsulating data within a class
b. Hiding the implementation details and showing only the functionality
c. Inheriting properties from a parent class
d. Providing multiple implementations of a function
2. Polymorphism allows objects to be treated as instances of their parent class. Which
type of polymorphism does this statement refer to?
a. Compile-time polymorphism
b. Run-time polymorphism
c. Overloading
d. Overriding
3. Which of the following is NOT true about abstract classes in Python?
a. Abstract classes can be instantiated directly.
b. Abstract classes can contain abstract methods.
c. Abstract classes can provide default implementations of some methods.
d. Subclasses of abstract classes must implement all abstract methods.
4. In Python, which module provides support for defining abstract base classes?
a. collections
b. abc
c. abstract
d. base
5. Which of the following statements best describes abstraction in object-oriented
programming?
a. Abstraction involves combining data and methods that operate on the data into a
single unit.
b. Abstraction means using the same function name for different purposes.
c. Abstraction is the process of hiding the implementation details and showing only
the functionality to the user.
d. Abstraction allows objects to be treated as instances of their parent class.
II.

1. Explain how abstraction helps in managing complexity in software development. Provide an


example to support your explanation.
- Abstraction in software development helps us handle complexity by allowing us to focus on
what something does rather than how it does it. Imagine you have a car. You don't need to know
every tiny detail of how the engine works to drive it. Instead, you interact with abstract concepts
like the steering wheel, pedals, and gear shift, which hide the complex inner workings of the car.
This way, you can use the car effectively without being overwhelmed by its internal complexity.

2. Describe polymorphism and how it can be achieved in Python. Provide an example using a
class hierarchy.
- Polymorphism is like having a single name with multiple forms. In Python, we can achieve
polymorphism through method overriding in inheritance. Let's say we have a class hierarchy
with a base class called Animal and two subclasses called Dog and Cat. Each subclass can have
its own implementation of a method, let's say make_sound(). When we call make_sound() on an
object of type Animal, it behaves differently based on whether the object is a Dog or a Cat,
exhibiting polymorphic behavior.

3. What are the main differences between abstraction and encapsulation? Provide examples to
illustrate your points.
- Abstraction and encapsulation are both ways of managing complexity in software, but they
serve different purposes. Abstraction focuses on hiding the implementation details and showing
only the essential features, like driving a car without knowing how the engine works.
Encapsulation, on the other hand, involves bundling the data and methods that operate on the
data into a single unit, like putting all the car's controls and mechanisms inside its body. So,
while abstraction shields us from complexity by showing only what's necessary, encapsulation
protects the inner workings of an object, preventing direct access and manipulation from outside.
III.

Discuss the importance of abstraction and polymorphism in object-oriented design. How


do these concepts contribute to software maintainability, scalability, and reusability? Provide
examples from your experience or from theoretical scenarios.

Abstraction and polymorphism are like the superheroes of object-oriented design,


swooping in to save the day when it comes to creating software that's easy to maintain, scale, and
reuse.
Imagine you're building a software system that simulates a zoo. Abstraction allows you to
focus on the essential aspects of each animal without getting bogged down in the nitty-gritty
details. For instance, instead of worrying about whether a lion has fur or scales, you abstract
away those specifics and focus on common behaviors like eating and sleeping. This abstraction
simplifies the design process and makes it easier to manage the complexity of the system.
Now, let's talk about polymorphism. Picture a scenario where you have different types of
animals in your zoo: lions, tigers, and bears (oh my!). Polymorphism allows you to treat each of
these animals as instances of a broader category, such as Animal. So, when it's feeding time at
the zoo, you don't need separate feeding routines for each animal type. Instead, you can use a
single method like feed (Animal animal) that adapts its behavior based on the specific type of
animal it's feeding. This flexibility provided by polymorphism makes your code more adaptable
and easier to scale as you add new types of animals to your zoo.
Now, let's bring it back to the bigger picture. In the world of software development,
maintainability is crucial because as your codebase grows, you want to be able to make changes
without causing a tangled mess. Abstraction and polymorphism help by providing clear
boundaries between different components of your system and allowing you to make changes in
one place without affecting the rest of the codebase.
Scalability is all about being able to handle increased workload or complexity without
breaking a sweat. Abstraction and polymorphism contribute to scalability by allowing you to
build modular, reusable components that can be easily expanded or modified as needed.
And finally, reusability is like recycling for your code. Abstraction and polymorphism
enable you to build components that can be reused in different parts of your system or even in
entirely different projects. For example, once you've implemented a generic Animal class with
feeding and sleeping behaviors, you can use it not only in your zoo simulation but also in other
projects that involve animals.
So, in summary, abstraction and polymorphism are essential tools in the object-oriented
designer's toolkit, helping to create software that's easier to maintain, scale, and reuse, much like
superheroes saving the day in the world of software development.

You might also like