Professional Documents
Culture Documents
COMSATS University Islamabad: Sahiwal Campus (Department of Computer Science)
COMSATS University Islamabad: Sahiwal Campus (Department of Computer Science)
Sahiwal Campus
(Department of Computer Science)
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. }
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. {
34. System.out.println(“TestShape”);
35. }
36. }
The End