← Back to Polymorphism
Question 662
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 the output shown in Expected Output.
Expected Output:
28= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Polymorphism
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · 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.