0% found this document useful (0 votes)
8 views13 pages

OOP Concepts: Classes and Exercises

This worksheet provides exercises on Object-Oriented Programming (OOP) concepts, including definitions and principles like abstraction, encapsulation, polymorphism, and inheritance. It includes practical applications such as designing a School Management System, implementing classes, and demonstrating key OOP features through coding exercises. The document emphasizes the importance of encapsulation, method overriding, and abstract classes in software design.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

OOP Concepts: Classes and Exercises

This worksheet provides exercises on Object-Oriented Programming (OOP) concepts, including definitions and principles like abstraction, encapsulation, polymorphism, and inheritance. It includes practical applications such as designing a School Management System, implementing classes, and demonstrating key OOP features through coding exercises. The document emphasizes the importance of encapsulation, method overriding, and abstract classes in software design.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Worksheet – OOP: Classes, Objects, and

Advanced Concepts
This worksheet contains exercises to help you practice Object-Oriented Programming
(OOP) concepts based on Modules 4, 5, and 6. Answer the questions in the spaces provided.

Part A: Conceptual Questions


 1. Define Object-Oriented Programming (OOP) in your own words. How does it differ
from Procedural Programming?

Object-Oriented Programming is easier to understand if we compare it to


something familiar. For example, a toy car. The toy car has features like color, size, and
brand, and it can also do actions like moving forward, stopping, or honking. In OOP, that toy
car is like an object because it keeps both the details (information) and the actions
(functions) in one place.
On the other hand, Procedural Programming works more like giving step-by-
step instructions. You first say push the car, then stop it, then honk. It works, but the actions
and information are separated. That is why OOP is helpful, because it keeps things more
organized and makes it easier to work with programs.

 2. Explain the following OOP principles with a real-world analogy:


- Abstraction
Abstraction in OOP is about hiding the complicated details and only
showing what is important. For example, when we drive a car, we only focus on the steering
wheel, pedals, and gearshift. We don’t need to know how the engine or the brakes really
work inside. The car hides those things from us and just lets us use what we need. This is
the same with abstraction in programming.

- Encapsulation
Encapsulation is about keeping the data and methods together while also
protecting them from outside interference. For example, a TV remote. We can press the
buttons to control the TV, but we don’t see or touch the circuits and wires inside. Everything
is already packaged and safe inside the remote.

- Polymorphism
Polymorphism is when one thing can have many forms. The word “run” is a
good example because we can use it in different situations: a person can run in a race, a
program can run, and even a refrigerator can run.
- Inheritance
In Mobile Legends, heroes belong to classes like Marksman, Tank, Assassin,
or Mage. A class gives them general traits. For example, all marksmen usually have high
damage but low defense. This is an example of inheritance, so in short, inheritance means
passing down traits from one class to another.

 3. Differentiate between a class and an object. Give two real-world examples.


A class in OOP is like a blueprint or design. It tells us what features
something should have and what actions it can do, but it is not the actual object. It only
serves as a guide or plan for creating objects. While an object is the real thing made from the
class. It has actual details and can be used in real situations, unlike the class which is just an
idea.

For example, a class “Car” can describe that cars have colors, brand, and
actions like drive or stop. But the actual object is a specific red Toyota car parked outside
your house. Another example is a class “Dog,” which describes that a dog has a name, breed,
and actions like bark or eat. The object would then be a brown Labrador named Blackie that
plays in your yard. These examples show how classes and objects are connected.

 4. What is the role of the self keyword in Python classes?


In Python, the keyword self is used to represent the current object of a class.
It allows each object to keep and use its own data. For example, if we have a class Dog with
an attribute name, using [Link] makes sure that each dog object has its own name. If we
create one dog named “Blackie” and another named “Brownie,” self helps the program know
which name belongs to which dog. Without self, the class would not know which object’s
information to use. In short, self connects the data and actions to the exact object we are
working with.

 5. Explain the difference between default constructor and parameterized constructor


with Python examples.
A default constructor does not take any extra arguments other than “self”. It
usually just set default values. For example:
A parameterized constructor takes arguments beside self and it allows us to
set values while creating the object. For example:

Part B: Application Exercises


 6. Suppose you are designing a School Management System:
- Which classes would you create?
- List at least three attributes and three methods for the Student class.
If I am designing a School Management System, I would create classes such
as Student, Teacher, Subject, Classroom, and Grade. These classes represent the important
parts of the school that need to be managed. For example, the Teacher class can keep track
of teacher details, while the Subject class can handle the subjects offered in school. The
Student class is one of the most important because it stores information about students and
allows them to interact with the system.

For the Student class, three attributes can be: name, studentID, and course.
The name is the basic information used to identify the student, while the studentID is a
unique number given to each student so that the system can distinguish them. The course
attribute shows what program or track the student is taking. For the methods, we can have
enrollSubject(), viewGrades(), and payTuition(). The enrollSubject() method allows the
student to register for subjects in a semester, the viewGrades() method lets them see their
grades online, and the payTuition() method is for recording or updating their tuition
payments. These attributes and methods make the Student class complete and useful in a
school system.

 7. In the context of Encapsulation, explain why direct access to private variables should
be avoided. Give an example using getter and setter methods.

In Encapsulation, direct access to private variables should be avoided
because it can make the data unsafe and unorganized. If other parts of the program can
change the value directly, there is a risk of errors or wrong values being stored. By using
getter and setter methods, we can control how the data is accessed or updated. This way,
the variables are protected, and only valid changes can be made.
 8. Explain method overriding in Python with the example of a Rectangle and Square
class. Why is overriding useful?

Method overriding in Python happens when a child class defines a method
that already exists in its parent class, but gives it a different implementation. This allows the
child class to change or “override” the behavior of the parent class to fit its own needs.
For example:

Part C: Coding Exercises


 9. Create a class Person with attributes name and age. Add a method introduce() that
prints a self-introduction. Then, create two different objects of the class.
10. Write a program in Python to demonstrate inheritance using a base class Polygon and a
derived class Triangle. The Triangle class should have a method to calculate the area using
Heron’s formula.


11. Implement a class method called fromBirthYear() in the Person class that calculates age
from the year of birth. Test it by creating an object.

12. Implement a Mobile class with a static variable for discount percentage. Add methods to
enable or disable discount and calculate final price for a mobile phone. Show how the
discount applies differently depending on the brand/price.


 13. Write a Python program to demonstrate polymorphism where different classes
(Dog, Bear) implement the same method sound(). Call the method on different objects.

Part D: Problem-Solving & Critical Thinking


 14. A company has two classes: Employee and Salary. Use composition to design these
classes so that each employee has an associated salary object. Write a program that
calculates the annual salary with bonus.
 15. A Customer class has a has-a relationship with an Address class. Demonstrate
aggregation by creating multiple customers that can share the same address object.
Explain why aggregation is different from composition.

16. Write a Python program to demonstrate the use of the super() function in
inheritance. Explain why it is useful compared to directly calling the parent constructor.

17. Why are abstract classes important in software design? Implement a base abstract
class Document with an abstract method show(), and then implement two subclasses
Pdf and Word that override this method.

Abstract classes are like templates in programming. They give a common structure
for other classes to follow but don’t provide all the details. This means every subclass must
follow the same rules, but they can still decide how to do things in their own way. In short,
abstract classes keep code consistent while allowing flexibility.

You might also like