Abstract Classes and Interfaces

Download Report

Transcript Abstract Classes and Interfaces

Abstract Classes and
Interfaces
Week 17
Abstract Classes and Interfaces
CONCEPTS COVERED THIS WEEK

Computer simulations
 Abstract methods
 Abstract classes
 Interfaces
 Multiple inheritance
Simulations
Programs are regularly used to simulate
real-world activities
•They are often only partial
simulations
•They often involve simplifications.
–Greater detail has the potential to
provide greater accuracy
–Greater detail typically requires more
resources
Benefits of simulations
• Support useful prediction.
– The weather.
• Allow experimentation.
– Safer, cheaper, quicker.
• Example:
– ‘How will the wildlife be affected if we cut a
highway through the middle of this national
park?’
Predator-prey simulations
• There is often a delicate balance between
species.
– A lot of prey means a lot of food.
– A lot of food encourages higher predator
numbers.
– More predators eat more prey.
– Less prey means less food.
– Less food means ...
The foxes-and-rabbits project
Main classes of interest
• Fox
– Simple model of a type of predator.
• Rabbit
– Simple model of a type of prey.
• Simulator
– Manages the overall simulation task.
– Holds a collection of foxes and rabbits.
Example of the visualization
The Animal superclass
• Fox and Rabbit have a superclass called
Animal, which contains common fields
such as alive and location
• Fox and Rabbit have different versions
of a method called act, which models
their behaviour at each time step
The act method of Animal
• Static type checking requires an act
method in Animal
• Define act as abstract:
abstract public void act(List<Animal> newAnimals);
The Animal class
public abstract class Animal
{
fields omitted
/**
* Make this animal act - that is: make it do
* whatever it wants/needs to do.
*/
abstract public void act(List<Animal> newAnimals);
other methods omitted
}
Abstract classes and methods
• Abstract methods have abstract in the
signature.
• Abstract methods have no body.
• The presence of at least one abstract
method makes the class abstract.
• Abstract classes cannot be instantiated.
• Concrete (i.e. non-abstract) subclasses
complete the implementation.
Further abstraction
Selective drawing
(multiple inheritance)
Multiple inheritance
• Having a class inherit directly from multiple
ancestors.
• Each language has its own rules.
– How to resolve competing definitions?
• Java forbids it for classes.
• Java permits it for interfaces.
– No competing implementation.
Interface
• interface
– A Java programming language keyword used
to define a collection of method definitions
and constant values. It can later be
implemented by classes by means of the
"implements" keyword.
Interfaces as method specifications
• Interfaces specify method signatures only
• Each method signature is followed by a
semi-colon (;)
An Actor interface
public interface Actor
{
fields omitted
/**
* Perform the actor's daily behavior.
*/
void act(List<Actor> newActors);
other methods omitted
}
Features of interfaces
•
•
•
•
All methods are abstract.
There are no constructors.
All methods are public.
All fields are public, static and final.
Multiple interfaces
• Because interfaces simply specify method
signatures, a single class can implement
several different interfaces in order to
ensure more methods can be
implemented.
Classes implement an interface
public class Fox extends Animal implements Drawable
{
...
}
public class Hunter implements Actor, Drawable
{
...
}
Implementing an Interface
• When a class implements an interface, it is
essentially signing a contract.
– Either the class must implement all the
methods declared in the interface and its
superinterfaces, or the class must be declared
abstract.
– The method signature in the class must match
the method signature as it appears in the
interface. A class that implements the
ActionListener interface must contain the
method actionPerformed
Interfaces as types
• Implementing classes do not inherit code,
but ...
• ... implementing classes are subtypes of
the interface type.
• So, polymorphism is available with
interfaces as well as classes.
Interfaces are useful for the
following:
• Capturing similarities among unrelated
classes without artificially forcing a class
relationship
• Declaring methods that one or more
classes are expected to implement
• Modelling multiple inheritance, a feature of
some object-oriented languages that
allows a class to have more than one
superclass