Abstract Classes



As you move upwards in an inheritance hierarchy:


Example
: Electronic message system
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.



public abstract class Message
{
    private String sender;

   public Message(String from)
   {
       sender = from;
   }

   public abstract void play(); // This method cannot be defined in this class.
...

 
}


Example:
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:

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
}



Differences - Interfaces and Abstract Classes


Question:
How does an interface differ from an abstract class?

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).


Example:

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
}


Example:
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.


Extending an Interface


An interface can extend another interface, just like a class can extend another class:

Example:
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.


Example:
interface GasEngineDriven extends EngineDriven, SomeOtherInterface
{
...
}





UML for interfaces


Example (in class)