Lesson9-Abstract Classes Classes
Lesson9-Abstract Classes Classes
Simply put, an abstract class is a Java class that has not been fully
defined. For example, it may have the implementation of a method
missing.
This can be useful, as it allows the subclasses to inherit and override
these abstract methods; and it gives each of these subclasses a
consistent programming interface.
Furthermore, the “gap” that the abstract class fills in the class hierarchy
can be used as a generic type to handle any extension of the abstract
class.
Note: a fully implemented (normal) class is referred to as a concrete
class.
Slide 2
Abstract Classes: an Example
Shape
Therefore, it would make
conceptual sense to give the A = ??
area method an implementation
only in the concrete subclasses;
we can make it abstract to
ensure this.
A=l*w
A=½b*h
Slide 3
Implementing an Abstract Class
Note the lack of an implementation for the area() method, and the need
to declare the class as abstract.
Slide 4
Extending an Abstract Class
Slide 5
Using Abstract Classes
If there were any abstract properties left in our subclass of Shape, that
subclass can be defined as abstract, and a compiler error can be avoided.
However, it is not possible to make an instance of an abstract class. In
order to make instances of a class, it must be fully defined.
With the class hierarchy having the abstract Shape as its parent, it allows
items common to all shapes (such as the colour) to be defined and
inherited, thus re-using code as much as possible; but it still allows us to
leave some implementation details for further down the class hierarchy.
Slide 6
Abstract Classes and Polymorphism (1)
Although abstract classes lack some implementation details, they can still
be used in polymorphic behaviour.
Shape s = r;
System.out.println(s.area());
Slide 7
Abstract Classes and Polymorphism (2)
Here is a more useful example; it is a method that will take any Shape
object, and print its area.
Recall earlier that we could not directly create an object from the abstract
Shape class. However in these cases, we are working on concrete
subclasses of Shape; we are just using the Shape data type as a handle
to refer to any subclass.
Remember the ‘is-a’ relationship: “a Rectangle is a Shape”
Slide 8
What is an interface?
Slide 9
Interfaces: an Example
Slide 10
Defining an Interface
interface Printable
{
public void print();
}
Slide 11
Implementing an Interface
Slide 12
An Interface as a Data Type
Much like earlier with the abstract class, we can use our Printable
interface as a data type:
This method will accept any object that implements Printable, and call
its print() method.
However, when referring to the objects as Printable, we can only call
what is defined inside the Printable interface.
Slide 13
An Example: the Shape class
The next step is to extend Shape to handle circles and rectangles, and
then create instances of these classes:
Slide 14
An Example: the Circle class
Circle(double r)
{
this.r = r;
}
Slide 15
An Example: the Rectangle class
Circle(double w, double h)
{
this.w = w; this.h = h;
}
Slide 16
Introducing Interfaces
Suppose we want to derive a subclass of Rectangle that allows a
rectangle to be drawn on a window object. We might also want to do this
with circles as well. What can we do?
Rewrite the Shape hierarchy to include drawing methods?
We would have to override these in each non-abstract shape.
Define drawing methods in each non-abstract shape?
More elegant, but no less work.
Can we define a class that extends Shape but also has a ‘parent’ of sorts,
that provides methods for drawing shapes in a window object? Java does
not allow multiple inheritance, but we can use an interface for this job.
interface DrawShape
{
public void setColor(Color c);
public void setPosition(double x, double y);
}
Slide 17
The DrawRectangle class
class DrawRectangle extends Rectangle implements DrawShape
{
private Color c;
private double x, y;
DrawRectangle(double w, double h)
{
this.w = w; this.h = h;
}
Slide 19
Abstract Classes vs. Interfaces
Slide 20
• END