[go: up one dir, main page]

0% found this document useful (0 votes)
11 views5 pages

(JAVA Assignment-T)

Uploaded by

2023000000148
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)
11 views5 pages

(JAVA Assignment-T)

Uploaded by

2023000000148
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/ 5

Q1: Create a base Course class in Java with attributes such as courseName, courseCode, and

creditHours. Now, create two subclasses: UndergraduateCourse: This subclass might include
additional attributes like generalEducationRequirement. PostgraduateCourse: This subclass
might include attributes such as researchComponent to indicate if the course involves a
research project. Explain how inheritance allows you to avoid redundancy when creating
these subclasses and how polymorphism could be used to manage different behaviors (e.g.,
calculating course difficulty differently for undergraduate and postgraduate courses).
Solution:
Q2: Create a base Student class with attributes such as studentID, name, and a list of enrolled courses.
Then, create two subclasses: UndergraduateStudent, which may have an attribute like advisorName
to represent the student's academic advisor. PostgraduateStudent, which may include attributes such
as thesisTopic for managing postgraduate research. Implement methods in the Student class such as
enrollInCourse() and dropCourse(), ensuring that these methods can be overridden by the subclasses
if necessary. Describe how polymorphism allows the system to manage different types of students
with a single method call (e.g., enrollInCourse()), even though the specific behavior may vary between
undergraduate and postgraduate students. Q3: Imagine a rule where UndergraduateStudents are
limited to enrolling in a maximum of 6 courses, while PostgraduateStudents can enroll in a maximum
of 4 courses per semester. Modify the Student class and its subclasses to enforce these different limits
using polymorphism and overriding. Provide a detailed explanation of how inheritance and
polymorphism allow you to handle these different rules without duplicating code.

Solution:
Explanation:

With inheritance, I define shared attributes and behaviors in a base class (like
Course) and only the unique features in each subclass.

Without using inheritance, I would need to repeat common attributes and methods
in each class. For example, both UndergraduateCourse and
PostgraduateCourse have:

• courseName
• courseCode
• creditHours
• and likely a method like getCourseDetails()

If written separately, I would be duplicating this code in multiple classes — which


makes maintenance harder and leads to bugs when one class is updated and the
other isn't.

Polymorphism:
Polymorphism allows different classes (like UndergraduateStudent,
PostgraduateStudent) to be treated as if they are the same type (Student) but
they behave differently based on the actual object.

From the above code, I calling the same method enrollInCourse(), this behavior
differs based on the actual object (UG or PG). This is runtime polymorphism
(method overriding).

Main File:

C:\Users\Lenovo\OneDrive\Documents\NetBeansProjects\Mobile
User\src\main\java\Assignment

You might also like