1. LAB 9+10: INHERITANCE, INTERFACE AND POLYMORPHISM
Objectives:
Understanding Unified Modeling Language (UML)
Applying Inheritance mechanism (is-a relation)
Understanding and using super keyword
Using Abstract classes and methods
Applying methods overriding mechanism
Interface
Polymorphism
Practice – Shape hierarchy UML:
2. Classes’ description:
Point class members:
Fields: x and y as integers
Methods:
o Default and parametrized constructors
o Setters and getters
o toString(): returns a string representation of the class attributes values; (i.e. (100,200))
Shape is an abstract class with the following members:
o Fields:
point as Point
count as static integer
o Methods:
Default and parametrized constructor
Setters and getters
toString(): returns a string representation of the class attributes values
Circle class – extends Shape class – members:
o Fields:
Inherited fields from shape class
radius as double
count: as static integer
o Methods:
Default and parametrized constructor
Setters and getters
diameter(): returns the diameter of the circle
toString(): returns a string representation of the circle attributes values
Ex: Center: (100,150), Radius: 15.5
Rectangle class – extends shape class- members:
o Fields:
Inherited fields from shape class
Width and height as doubles
count: as static integer
o Methods:
Default and parametrized constructor
Setters and getters
toString(): returns a string representation of the rectangle attributes values
Ex: starting point: (100,150), width: 20, height: 56.25
Square class – extends Rectangle class- members:
o Fields:
Inherited fields from rectangle class
count: as static integer
o Methods:
Default and parametrized constructor
Setters and getters
toString(): returns a string representation of the square attributes values
Ex: starting point: (100,150), edge: 20
3. Note: the static member count declared in each class used to store number of objects created from that
class and its value shared among all the objects of the same class
Design a JFrame class as figure below, which allows the user to:
In the input tab:
add circles, rectangles and squares to ArrayList of type Shape, and show them on a
corresponding JList as the figure below
display number of objects created of each type
in the preview tab:
select a list member to print that shape area and perimeter on a message dialog; if the shape is
circle then print its diameter also.
Application frame:
4. Using Polymorphism:
Replace the preview tab with following tab to handle the shapes using the polymorphism approach by
creating an Array (or ArrayList) of the Shape class (superclass) and add object references of the
subclasses.
In this tab all the shapes are shown in the same list, then when:
select a list member to print that shape area and perimeter on a message dialog; if the shape is
circle then print its diameter also.
Click:
o edit shape button will asks the user to enter new values for the selected shape
attributes through input dialogs and sets these values to it
o remove shape button will remove the selected shape from the array list
o find similar shapes button show all the similar shapes in a message dialog