0% found this document useful (0 votes)
5 views21 pages

Lesson9-Abstract Classes Classes

abstract classes

Uploaded by

2106279
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
5 views21 pages

Lesson9-Abstract Classes Classes

abstract classes

Uploaded by

2106279
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 21

• Abstract classes and Interfaces

What is an abstract class?

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

Take the example of a series of shapes, that is to be modelled in a Java


class hierarchy.
All shapes will have a colour, and other common elements, however (for
example) the manner in which the area of the shape is calculated would
depend on what kind of shape it is.

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

An abstract class (or an abstract property) is defined using the keyword


abstract. It is also possible to define an abstract class that has no
abstract properties!

abstract class Shape


{
Color col;

public abstract double area();


}

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

An extension of an abstract class only becomes concrete if there are no


abstract properties left. In our Rectangle class we must therefore
override the area() method:

class Rectangle extends Shape


{
double length, width;

public double area()


{
return (length * width);
}
}

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.

Shape s = new Shape(); // this will not work


Rectangle r = new Rectangle(); // this is allowed

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.

Rectangle r = new Rectangle();


r.length = 2.5;
r.width = 5.0;

Shape s = r;

System.out.println(s.area());

This code will actually work! This is because there is actually a


Rectangle object inside the Shape reference s. However, from s, we
can only call methods declared inside Shape.

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.

public void printArea(Shape in)


{
System.out.println("Area: " + in.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?

An interface is a definition of a programming interface that classes can


implement. It is not a class.
It is possible for a class to implement many interfaces.

The major benefit of using an interface is that it allows otherwise


unrelated classes to be given a “common ground” without the need for
multiple inheritance (which Java forbids.)
If a class implements a particular interface, it must directly implement all
of the methods of that interface.
If a class that implements an interface is extended, those subclasses
also implement the interface. (This is because the methods that make
the implementation will be inherited.)

Slide 9
Interfaces: an Example

Consider a set of class hierarchies, some of whose objects can be printed


out to the screen.
We could create an interface defining how the “printable” functionality is
to be implemented, and have these classes implement that interface.
The benefit is that we can then use that interface as a data type, much
like we can with superclasses and polymorphism.

All Printable classes in the


diagram can be referred to by
their capability, even though
they are completely unrelated
in the hierarchy.

Slide 10
Defining an Interface

The code to define an interface is somewhat similar to an abstract class;


no method implementations are given, however there is no use of the
abstract keyword, and we are defining an interface, and not a
class.

interface Printable
{
public void print();
}

We can also extend an existing interface to make an even larger sub-


interface if need be. This is done using the extends keyword, much like
extending a class.

Slide 11
Implementing an Interface

In order to get a class to implement the interface, we use the


implements keyword:

class StringData implements Printable


{
String data;

public void print()


{
System.out.println("Data: " + data);
}
}

We must provide an implementation for print() here.

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:

public void printObject(Printable in)


{
in.print();
}

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

Let’s write a more formal example of abstract classes and interfaces.


Specification: define a set of classes to process geometric shapes. There
is no basic shape from which all shapes derive, so we place an abstract
class at the root of the hierarchy.
abstract class Shape
{
public abstract double area();
public abstract double circumference();
}

The next step is to extend Shape to handle circles and rectangles, and
then create instances of these classes:

Rectangle aRect = new Rectangle();


Circle aCircle = new Circle(3.0);

Slide 14
An Example: the Circle class

class Circle extends Shape


{
protected double r;

Circle(double r)
{
this.r = r;
}

public double area()


{
return Math.PI * r * r;
}

public double circumference()


{
return 2 * Math.PI * r;
}
}

Slide 15
An Example: the Rectangle class

class Rectangle extends Shape


{
protected double w, h;

Circle(double w, double h)
{
this.w = w; this.h = h;
}

public double area()


{
return w * h;
}

public double circumference()


{
return 2 * (w + 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;
}

public void setColor(Color c)


{
this.c = c;
}

public void setPosition(double x, double y)


{
this.x = x; this.y = y;
}
}
Slide 18
Design with Abstract Classes and Interfaces

Why abstract classes?


We want a generic class that nobody can instantiate.
Why interfaces?
• a generic class that nobody can instantiate; and
• multiple inheritance without conflict in implementation code.

Note that a generic class ensures a uniform design throughout its


subclasses. That is, we are guaranteed that a method defined in the
generic class can be invoked on an object of any of its subclasses.
Recall that generic classes, together with polymorphism and dynamic
binding, promote code reuse by allowing methods to be called in a
generic way.

Slide 19
Abstract Classes vs. Interfaces

A side-by-side comparison of the two:

Abstract Class Interface

1. Used by extending. 1. Used by implementing.


2. Can have abstract and concrete 2. Can have only abstract methods.
methods.
3. All methods must be implemented.
3. Need not implement all abstract
4. Data has to be public static final
methods.
constants.
4. Can have any modifiers on data
5. A class can implement many
types.
interfaces.
5. A class cannot extend multiple
6. Cannot be instantiated.
abstract classes.
6. Cannot be instantiated.

Slide 20
• END

You might also like