โ Back to Polymorphism
Question 572
Build a Shape Hierarchy
Build a Shape Hierarchy
Build a polymorphic shape hierarchy from scratch. Define anabstractclassShapewith an abstract methodint Area(). Then define three subclasses, each with a constructor and anArea()override: -Square(int side)with areaside * side-Rectangle(int w, int h)with areaw * h-Triangle(int b, int h)with areab * h / 2The tester stores them in aList<Shape>and sums their areas. Do NOT write aMainmethod. ForSquare(2),Rectangle(3, 4), andTriangle(6, 4)(areas 4, 12, 12) the program prints: 28
Expected Output:
28
Topics:
Polymorphism
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.