Abstract Classes
As you move upwards in an inheritance hierarchy:
- Classes become more general and more abstract
- The classes may become more a framework for other classes than a
class
you want to instantiate
Example: Electronic message system
- System handles email, faxes and voice mail
3 classes needed: TextMessage, VoiceMessage, FaxMessage
A mailbox contains messages of all 3 types, so we access them through
references
to a general parent class called Message.
Move common operations to the Message class: for example, all messages
have
a play() method.
But how do you implement play() in the parent class? You cannot.
- Use the abstract keyword when a method cannot be defined in the
class.
- A class with one or more abstract methods is called an abstract
class
(and must be declared abstract).
public abstract class Message
{
public Message(String from)
{
sender = from;
}
public abstract void play(); // This method cannot be
defined
in this class.
...
private String sender;
}
- Even though the Message class is abstract, it may contain
concrete
methods and data.
- The non-abstract subclasses of our abstract class must implement
the
abstract methods.
- Abstract classes cannot be instantiated. Instance variables of an
abstract
class type can be declared, but they will refer to an instance of a
non-abstract
subclass.
Ex: Message myMsg = new VoiceMessage("greet.au");
UML Notation: The names of abstract classes and methods are italicized
in UML, or instead you can include the word abstract in curly braces.
Example:
class TextMessage extends Message
{
public TextMessage(String from, String text)
{
super(from);
this.text = text;
}
public void play()
{
System.out.println(text);
}
private String text;
}
Interfaces
An interface:
- extends the idea of an abstract method and class
- contains only prototypes for a collection of methods (no
implementation
for any method)
- defines a collection of methods that a class must implement
- A class advertises that it implements an interface, and
then
implements the required methods
Syntax:
public interface InterfaceName
{
// constants - All variables in an interface are
explicitly
or implicitly public class constants (that is, public static final)
// method declarations - all methods are explicitly or
implicitly
public
}
How does an interface differ from an abstract class?
- an interface cannot specify any method implementations
- all methods in an interface are public
- All variables in an interface are public static final
- A class can implement multiple interfaces, but can only extend
one
parent class
- Unrelated classes can implement the same interface and objects of
those
classes have a common type - the interface type
You can: declare variables to be of an interface type, you can declare
arguments
and return types of methods to be an interface type.
An interface looks like a purely abstract class (that is, a class with
only
abstract methods).
Ex:
interface EngineDriven
{
boolean startEngine(); // methods do not have to be
declared
abstract - they are automatically abstract
void stopEngine(); methods do not need to be
declared
public - they are automatically public
float accelerate(float acc);
boolean turn(Direction dir); // Direction is a class
}
Ex: A class that implements all of EngineDriven's methods can declare
that
it implements the interface.
class Automobile implements EngineDriven
{
...
public boolean startEngine()
{
if(notTooCold)
engineRunning = true;
}
public void stopEngine()
{
engineRunning = false;
}
public float accelerate(float acc)
{
...
}
public boolean turn(Direction dir) {....}
}
Other classes that might implement EngineDriven? LawnMower,
MotorizedScooter
The interface defines a new type - EngineDriven. We can declare
variables
of this type and assign them any instance of an EngineDriven object:
Ex:
EngineDriven auto = new Automobile();
LawnMower mower = new LawnMower();
EngineDriven vehicle;
vehicle = auto;
vehicle.startEngine();
vehicle.stopEngine();
vehicle = mower;
vehicle.startEngine();
vehicle.stopEngine();
Since Automobile and LawnMower both implement EngineDriven, objects
auto
and mower are considered to be of type EngineDriven.
An interface can extend another interface, just like a class can extend
another
class:
Ex:
interface GasEngineDriven extends EngineDriven
{
void setFuelGrade();
}
This interface extends EngineDriven and adds one additional method
prototype.
A class that implements GasEngineDriven must implement all methods in
both
interfaces.
An interface can extend multiple interfaces.
Ex:
interface GasEngineDriven extends EngineDriven, SomeOtherInterface
{
...
}
UML for interfaces:
Example (in class)