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
getandprotected setfor the area - a constructor that take the position of that
Shapeas a parameter - a
protectedmethod 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
privatefield andpublicfor 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
Circleas 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 static2 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.