Build a Shape Hierarchy
hardQuestion 662Build a polymorphic shape hierarchy from scratch. Define an abstract class Shape with an abstract method int Area(). Then define three subclasses, each with a constructor and an Area() override:
- Square(int side) with area side * side
- Rectangle(int w, int h) with area w * h
- Triangle(int b, int h) with area b * h / 2
The tester stores them in a List<Shape> and sums their areas. Do NOT write a Main method.
For Square(2), Rectangle(3, 4), and Triangle(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, so 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.