Labs
We will explore abstract classes, and create a class hierarchy where common fields are encapsulated in an abstract parent class.
AbstractClasses/com.example.abstractclasses.labs.vehicles- Make both the
VehicleandFlyingVehicleclassesabstract. Do you have to change any other application code?
(Solution: VehicleApp.java)
- Create an abstract class named
Shape. Provide privateintfields for x and y coordinates of the shape. Add getters and setters as well as appropriate constructors (including no-arg constructor). Add anabstractmethod calledgetArea.
(Solution: Shape.java)
- Develop two child classes of
Shape:RectangleandCircle. ARectanglehas fields of typedoubleforwidthandheight; aCirclehas aradius. Provide getters and setters for each field and appropriate constructors.
Implement the getArea method in each subclass. (Hint: Find java.lang.Math in the Java API documentation for usage information on Math.PI and the Math.pow() method for calculating the circle's area from its radius.)
(Solutions: Rectangle.java, Circle.java)
- Write a program that contains a
mainmethod that creates an array of threeShapes. Store twoRectangles and aCirclein the array. Pass thisShapearray to a method that loops through the array, printing out the area of each shape.
(Solution: ShapeApp.java)