Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

COMSATS University Islamabad

Sahiwal Campus
(Department of Computer Science)

Course Title: Advanced OOP Course Code: CSC446 Credit Hours: 3


Course Instructor: Dr. Muhammad Farhan Programme Name:
Semester: Batch: Section: Date: 20-07-2020
Time Allowed: 1.5 Hours Maximum Marks: 30
Student’s Name: Talha Alam Reg. No. CUI/ FA16-BCS-136 /SWL
Important Instructions / Guidelines:
Read the question paper carefully and answer the questions according to their statements.
Mobile phones are not allowed. Calculators must not have any data/equations etc. in their memory.

Sessional-II Examinations Spring 2020


Question # 1. [10+5+5]
Recall the concept of inheritance for the following diagram and answer the questions:

1. Show the inheritance of the Teacher and Administrator classes from the CommunityMember
class. There is no need to show the member variables and methods of the classes, but you must
write all the classes’ bodies.
2. If the Staff class has a method workingHour( ) then can we use it in Administrator class by
using inheritance?
3. Identify the type of inheritance shown in the diagram. Explain the type of inheritance and
why JAVA does not support other type of inheritance.
Answer:

1.
1. Public class CommunityMember {
2.
3. }
4.
5. public Class Employee extend CommunityMember {
6.
7. }
8.
9. Public class Faculty extend Employee {
10.
11. }
12.
13. Public class Administrator extend Faculty {
14.
15. }
16.
17. Public class Teacher extend Faculty {
18.
19. }

2. No, it can’t be used in Administrator class. Because Administrator class is inheriting


Faculty which further inherit Employees and so on. Administrator class does not inherit
Staff class in this.
3. This is a multilevel inheritance in the diagram.
Java does not support Multiple inheritance because java doesn't support multiple
inheritance directly because it leads to overriding of methods when both extended classes
have a same method name.

Question # 2. [10]
Write the code for the following scenario:
Parent class: Shape
Child: TwoDimensionalShape and ThreeDimensionalShape
Child of TwoDimensionalShape: Circle, Square and Triangle
Child of ThreeDimensionalShape: Sphere, Cube and Tetrahedron
Also create a TestShape class and show the polymorphic behaviour.

Answer:
1. Public Class Shape
2. {
3. public void print(){
4. System.out.println(“Shape”);
5. }
6. }
7. Public class TwoDimensionalShape extends Shape
8. {
9. }
10. Public class ThreeDimensionalShape extends Shape
11. {
12. }
13. Public class Circle extend TwoDimensionalShape
14. {
15. }
16. Public class Square extends TwoDimensionalShape
17. {
18. }
19. Public class Triangle extends TwoDimensionalShape
20. {
21. }
22. Public class Sphere extends ThreeDimensionalShape
23. {
24. }
25. Public class Cube extend ThreeDimensionalShape
26. {
27. }
28. Public class Tetrahedron extend ThreeDimensionalShape
29. {
30. }
31. Public class TestShape extends Shape

32. {

33. public void print(){

34. System.out.println(“TestShape”);

35. }

36. }

37. public static void main(String[] args){

38. Shape shape =new TestShape();

39. shape. Print(); //This can print TestShape

40. shape =new Shape();

41. shape. Print(); //This can print Shape}

The End

You might also like