Feedback for these lessons is very much welcome. If you like or hate something about a lesson let us know, either on GitHub, via Mail or just tell us in person ;)
x
A06 - Shape class and derivation
Now we want to extend our Polygon example a bit more. This time we will use inheritance.1
Step 1
Create an class Shape
. This class needs the following member:
- a field for the position (
Point2D
) of theShape
- a property with
get
andprotected set
for the area - a constructor that take the position of that
Shape
as a parameter - a
protected
method for the calculation of the area- it should return -1
Step 2
Let your Polygon
class derive from Shape
. Change the internal structure of Polygon
so that there are no errors through the derivation.
Hint: Don’t forget the base
constructor.
Step 3
Create a new class Circle
that also derive from Shape
. This class needs also the following member:
- a
private
field andpublic
for the radius- if the radius changed (per
set
) it should recalculate the area
- if the radius changed (per
- a constructor that take the radius of that
Circle
as a parameter - override the implementation of the given calculation method for the area
Step 4
Change the polygon creation method in the Program
.
Create a new static
2 method in the Program
class to create a Circle
with user input.
The radius should never be less or equal 0.
Hint: For the time being will use the (0, 0) point for the position of the shapes.