COS20007 Semester Test
Name: Truong Dang Kien
Student ID: SWS01105
Task 2:
1. Describe the principle of polymorphism and how it was used in Task 1.
In object-oriented programming (OOP), polymorphism is a concept that allows objects
of different classes to be used interchangeably through a shared interface. This means
that objects of several types can be accessed and manipulated using the same set of
methods or properties, regardless of their specific class. Polymorphism enables a single
function or method to work with objects of different classes if they share a common
interface or base class. This feature enhances code flexibility, reusability, and
maintainability by allowing the same code to manage different types of objects without
the need for explicit type checking or casting.
How it was used in Task 1: In Task 1, polymorphism was implemented using the
abstract class Thing. The Thing class defines a common interface for the File and
Folder classes. Both File and Folder are inherited from Thing and implement the
abstract methods Size and Print. This allows File and Folder objects to be added to the
FileSystem and Folder collections and operated on through their common interface,
enabling methods like PrintContents and Add to work with any Thing object.
2. Consider the FileSystem and Folder classes from the updated design in
Task 1. Do we need both classes? Explain why or why not.
Yes, the Folder and FileSystem classes are required. Maintaining the overall structure
and organization is the responsibility of the FileSystem class, which represents the
entire file system and controls the top-level components. The FileSystem can have a
hierarchical structure, though, because the Folder class represents a directory that can
hold both files and other folders. They serve different functions: the FileSystem is
responsible for collection, whereas the Folder organizes the items in a hierarchical
manner.
3. What is wrong with the class name Thing? Suggest a better name for the
class and explain the reasoning behind your answer.
The class name Thing is general. It doesn't give any meaningful information about what
the class represents, making the code less readable and harder to understand. A better
name for the class that I would give could be FileItem. It is more specific and indicate
that the class represents an item within a file system.
4. Define the principle of abstraction, and explain how you would use it to
design a class to represent a Book.
Abstraction in OOP is the concept of hiding the complex implementation details of a
system and exposing only the necessary and relevant parts to the user. It allows
developers to manage complexity by breaking down a system into smaller, more
manageable pieces and focusing on the essential features of an object.
Designing a class to represent a Book using abstraction:
When coding a class to represent a Book, we should focus on the essential attributes
and behaviors that define a book, such as title, author, and the number of pages. The
class should provide methods to interact with these attributes, like printing the details of
the book, without exposing the underlying implementation to the user. This helps in
managing complexity and maintaining a clear interface for interacting with book objects.