Abstract Classes and Interfaces

Download Report

Transcript Abstract Classes and Interfaces

Lecture 3
Casting
Abstract Classes and Methods
Interfaces
Casting
A class hierarchy
Object
Bicycle
MountainBike
RacingBike
Q: Mountainbike is a ? of bicycle?
Q: what java keyword is used to do this hierarchy?
Q:Does the Bicycle class use this keyword ?
Casting and object types
• When you create an object, its type is the class from
which is was instantiated..e.g. ..
MountainBike myBike = new MountainBike();
• myBike is a MountainBike (And it’s also is a Bicycle
and an Object)
– (Why an object?)
• But, a Bicycle may be a MountainBike but not
necessarily…
– (could be a RacingBike)
Casting and object types
• Casting allows the use of an object of one type in
place of another type (down the hierarchy)
Bicycle yourBike = new MountainBike();
myBike = (MountainBike) yourBike;
**OK**
Cast
Object
Bicycle
MountainBike
RacingBike
You can check the type of an object..
if (yourBike instanceof MountainBike)
{
// Cast
MountainBike newBike = (MountainBike)yourBike;
}
Casting
• Why are we looking at casting/object types?
• Because it’s used in GUI programming
• Used to check which GUI element was
clicked etc… More later!
Abstract Class
• An abstract class represents a generic
concept
e.g. Shape, Food, Fruit, …
• it encapsulates the features that all kinds of
elements of the concept have,
e.g. Shapes have area, circumference
• these features are implemented as abstract
methods
e.g. area(), circumference()
• abstract methods cannot have an
implementation
meaningless at the conceptual, generic level
Abstract Classes
• An abstract class or method is defined by the
keyword abstract
– abstract class Shape {
Note: no body
…
abstract double area();
abstract double cicumference();
…
};
• Any class with an abstract method is
automatically abstract and must be declared
as such
Abstract Class
• An abstract class cannot be instantiated
• A subclass of an abstract class can be
instantiated only if it overrides each of the
abstract methods of its superclass and
provides an implementation for all of them
– this subclass is known as a concrete class
• If a subclass of an abstract class does not
implement all the abstract methods it inherits,
the subclass is itself abstract
• (Note: static, final and private methods cannot be
abstract as they cannot be overridden)
Abstract Class
• An abstract class effectively defines a
complete programming interface – providing
its subclasses with the method declarations
for all of the methods necessary to implement
that programming interface.
– i.e. It defines what methods the subclass is going
to have to implement…
• Note: an abstract method in Java is like a pure virtual
function in C++
Abstract Class Example
See CODE
public abstract class Shape
{
public abstract double area();
}
// note no body
Implementing the abstract class
__-__________________________________________-__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Public class Circle extends Shape
{
// put in attributes needed here...
public Circle (double radius)
{
// constructor code..
}
public double getRadius()
{
// code...
}
public double area()
{
}
//implement abstract method
// Code for area calculation...
Implementing the abstract class
class Rectangle extends Shape{
// put in attributes needed here...
// constructor
public Rectangle (double width,double height)
{
// constructor code..
}
public double area()
{
//implement area abstract method...
}
// other methods ..etc
}
Using the Shape Objects
Shape[] shapes = new Shape[3];
//setup array of shapes
// then.. Instantiate the various shapes to be
used..circle, square etc.
// then.. Calculate the total area of the shapes..
double totalArea=0;
// compute total area
for (int i=0; i<shapes.length; i++){
totalArea+=shapes[i].area();
• Dynamic method lookup is used
– the area of a circle is computed using the method
defined by Circle class
– the area of a rectangle is computed using the
method defined by the Rectangle class
Interfaces in java
Interfaces
• Consider another type of shape, one where the
centre point of the shape is known
abstract class CenteredShape {
// instance fields
double x;
double y;
}
class CenteredCircle extends CenteredShape {…}
etc…
• We want CenteredCircle to support area() and
circumference() methods already defined
without re-implementing the methods 
Problem, no multiple inheritance in java
Interfaces
• Solution  an interface
• An Interface is a simple a set of methods in
java
– Use the type Interface instead of class in the java
source code…
–
public interface myInterface
{
//method declarations
...
}
Interface
• An interface defines a protocol of behaviour
that can be implemented by any class
anywhere in the class hierarchy.
• It defines a set of methods but does not
implement them.
• A class that implements the interface agrees
to implement all the methods defined in the
interface, thereby agreeing to certain
behaviour.
Some Interface Rules
• an interface contains no implementation.. Just the
method names/signatures
• the methods of an interface are implicitly abstract (but
abstract qualifier not needed)
• the only fields allowed in an interface are constants
(i.e. declared static final)
• all methods of an interface are implicitly public
• an interface cannot be instantiated  no constructor
Interface Example
• The interface Centered is defined as
public interface Centered {
public void setCenter(double x,double y);
public double getCenterX();
public double getCenterY();
}
• It defines the methods that a Shape subclass
should implement if it knows the x,y coords of
its center point
Implementing an Interface
• A class uses the keyword implements to
implement one or more interfaces
public class className implements interfaceName {
// class field definitions
...
// class method definitions
...
// interface method implementations
...
}
public class class1 implements interface1,interface2{
...
}
Implementing an Interface
public class CenteredCircle extends Circle
implements Centered {
// field defns
double cx;
//center x coord
double cy;
//center y coord
// constructor
public CenteredCircle(double cx, double cy,
double radius){
super(radius);
this.cx=cx;
this.cy=cy;
}
Since the class CenteredCircle implements the circle interface,
what extra methods must the CentredCircle class include?
Implementing an Interface
// implementations for the interface methods
public void setCenter(double x, double y){
cx=x;
cy=y;
}
public double getCenterX(){
return cx;
}
public double getCenterY(){
return cy;
}
Interfaces.. A bit more…
• Before writing a class definition, determine the public
interface
– the set of services offered to users of the class
• Java allows us to take this one stage further, by
formally recording the interface as a Java interface
• a java interface is just a collection of abstract
methods (i.e. we state the signatures, but not the
bodies)
Using an Interface
• Where a class implements an interface
instances of the class can be treated as
objects of the interface type as well as objects
of their own class type
• E.g. instances of CenteredCircle and
CenteredRectangle can also be treated as
- instances of Shape as they extend Shape
- instances of Centered as they implement
Centered
Quiz…what did you absorb?
• Explain in your own words what an interface is? Why
is it needed in Java?
• Can an interface implement its methods? Can an
abstract class?
• Can an interface be implemented by classes from
different hierarchies? (e.g. a Bicycle class, and a
Circle class?
• Can a class have more than one superclass?
• Can a class implement more than one interface?
Stopped
here
Abstract Classes vs Interfaces
• Differences between abstract classes and
interfaces
– An interface cannot have any implementation,
whereas an abstract class can.
– An interface is not part of the class hierarchy.
Unrelated classes can implement the same
interface.
– A class can implement many interfaces but can
have only one superclass.