โ Back to Inheritance
Question 523
Shape Area Hierarchy
Shape Area Hierarchy
Build a small polymorphic hierarchy. TheabstractclassShapedeclaresint Area(). CreateRectangle(constructorint width, int height, areawidth * height) andTriangle(constructorint baseLen, int height, area(baseLen * height) / 2), each inheritingShapeand implementingArea(). The tester sums the areas in aShape[]. Do NOT write aMainmethod. For aRectangle(2, 3)and aTriangle(4, 3)the program prints: Total area: 12
Expected Output:
Total area: 12
Topics:
Inheritance
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cw, cr, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.