Lab 10 - Abstract Class & Interface
Lab 10 - Abstract Class & Interface
BS(CS)- Semester 02
(Fall 2020)
Shape.java
Note the 2 abstract methods in the following abstract class. Also note the presence of two non-
Circle.java
The first non-abstract child of Shape is Circle. Note that all abstract methods have been provided
with an implementation.
/*
* Circle.java
*/
public class Circle extends Shape {
private double radius;
Rectangle.java
CS Department, BUKC 3/9 Semester Fall 2020
CSL-210: Object-Oriented Programming Lab Lab10: Abstrac Class and Interface
/*
* Rectangle.java
*/
public class Rectangle extends Shape {
private double length;
private double width;
Square.java
/*
* Square.java
*/
public class Square extends Rectangle{
public Square(double length){
super(length, length);
}
}
TestShapes.java:
This class basically creates several Shape objects and prints them. Note the up-casting in the
assignment of the array randomShapes. Since randomShapes is of type Shape, any sub-
class of Shape can be assigned to it. Such a reference is called a polymorphic reference. Also
note the polymorphic call to the area() and perimeter() methods when the toString()
method of each one of the classes is called depending upon the actual shape.
The assignments here are done randomly to illustrate the point that the methods executed belong
to the actual object which is dynamically bound to the object. Consider the line highlighted at the
CS Department, BUKC 4/9 Semester Fall 2020
CSL-210: Object-Oriented Programming Lab Lab10: Abstrac Class and Interface
end: System.out.println(randomShapes[i]); Looking at the code, you cannot tell
which toString() method will be called as the assignment of objects, the length of the array and
even the dimensions of the shapes are randomly assigned only to be decided at runtime.
// TestShapes.java
import java.util.Random;
switch(assigner) {
case 0: randomShapes[i] =
new Rectangle(generator.nextDouble()*DIMENSION,
generator.nextDouble()*DIMENSION);
break;
case 1: randomShapes[i] =
new Circle(generator.nextDouble()*DIMENSION);
break;
case 2: randomShapes[i] =
new Square(generator.nextDouble()*DIMENSION);
break;
}
}
return randomShapes;
}
DownCasting:
CS Department, BUKC 5/9 Semester Fall 2020
CSL-210: Object-Oriented Programming Lab Lab10: Abstrac Class and Interface
Consider the following program. Here each shape object is being tested whether it’s a Circle,
Rectangle or a Square using the instanceof operator. According to that, it’s radius/length/width is
also being printed by downcasting it to the required object..
DownCastingShapes.java
/*
* DownCastingShapes.java
*/
public class DownCastingShapes{
public static void main(String[] args){
Shape[] randomShapes = TestShapes.createShape();
Exercises
Exercise 1
Area = ¼**(side)2
Perimeter = 3*side
Provide accessor methods for the sides. Test your class using the TestShapes and
DownCastingShapes classes.
CS Department, BUKC 6/9 Semester Fall 2020
CSL-210: Object-Oriented Programming Lab Lab10: Abstrac Class and Interface
Below we consider an extension to our Shape class. Here we discuss about elliptical shapes which
have properties in addition to area and perimeter which are of interest to us.
Consider the Shape called Ellipse. An ellipse is basically nothing but an elongated circle. A
circle has a radius. An ellipse has two measurements: the major axis (a) and the minor axis (b)
with a > b.
Minor
axis (b)
radius Major axis (a)
The measure of “roundness” of an ellipse is given by its eccentricity e where the value of e is always
between 0 and 1. For a circle e = 0. The higher the value of e, the higher is the deviation of the
ellipse from its roundness.
Eccentricity is associated with a lot of other shapes as well e.g. hyperbola, parabola, etc.
The various formulae for the ellipse are:
Note here that no access modifiers have been given for the method eccentricity. The reason is that all
methods in an interface are by default public and abstract. Now we can define our class Ellipse.
CS Department, BUKC 7/9 Semester Fall 2020
CSL-210: Object-Oriented Programming Lab Lab10: Abstrac Class and Interface
Ellipse.java
/*
* Ellipse.java
*/
public class Ellipse extends Shape implements Eccentric
{
double a, b;
public Ellipse(double s1, double s2){
if(s1 < s2) {
a = s2;
b = s1;
}
else {
a = s1;
b = s2;
}
}
Exercises
Exercise 1
By looking at the formulae for an ellipse, provide the missing code for all of the methods in the class
Ellipse including the toString() method. Test your program using the
TestShapes.java class. Your output should look as follows (for an ellipse with a = 10 and
b = 7) (values are randomly generated).
CS Department, BUKC 8/9 Semester Fall 2020
CSL-210: Object-Oriented Programming Lab Lab10: Abstrac Class and Interface
Square
Area=100.0
Perimeter=40.0
Ellipse
Area=219.9114857512855
Perimeter=53.8212680240788
Eccentricity=0.714142842854285
Press any key to continue...
Exercise 2
How about the following class Circle.Since a Circle is a special case of an Ellipse, will the output of
TestShapes.java be affected if the following class is used instead of the class Circle used previously:
Circle.java
public class Circle extends Ellipse {
public Circle(double radius){
super(radius, radius);
}
}
<<I n t e r f a c e >>
Eccent r i c Shape
El l i ps e
Rect angl e
Ci r cl e Squar e
Project
From the past weeks you have found the relationships between the classes and also found the
inheritance and polymorphism in your classes. Try to decide about the abstract classes and interfaces
for your sub classes