bung Softwareentwicklung 2 fr Wirtschaftsinformatik - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

bung Softwareentwicklung 2 fr Wirtschaftsinformatik

Description:

An interface is a device or a system that unrelated entities use to 'interact' ... static methods not allowed in interface defintions. Softwareentwicklung 2 UE ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 12
Provided by: tkUnil
Category:

less

Transcript and Presenter's Notes

Title: bung Softwareentwicklung 2 fr Wirtschaftsinformatik


1
Übung Softwareentwicklung 2für
Wirtschaftsinformatik
UE03Interfaces
2
Introduction
  • An interface is a device or a system that
    unrelated entities use to "interact"
  • remote control, English language
  • In Java, an interface is a device that unrelated
    objects use to interact with each other
  • analogous to a protocol
  • An interface is a contract in the form of a
    collection of method and constant declarations.
  • When a class implements an interface, it promises
    to implement all of the methods

3
What is an interface?
  • An interface is related to an abstract class in
    that it has no implementation.
  • An interface is a type definition.
  • We use inheritance when we want to say that a
    class is a certain kind.
  • We use interfaces when we want to say that a
    class has a behavior.
  • An interface defines functionality which we want
    to make available through polymorphism.

4
How interfaces differ from classes?
  • public abstract class Shape public abstract
    double area() public double circumference()
  • class Circle extends Shape protected double
    r public Circle() r 1.0 public double
    area() return PI r r ...
  • class Rectangle extends Shape protected
    double w, h public Rectangle() w 1.0
    h 1.0 public double area() return w
    h ...

Shape
Circle
Rectancle
5
How interfaces differ from classes?
  • class DrawableCircle extends Shape protected
    double r public DrawableCircle() r
    1.0 public double area() return PI r
    r public void draw() ...
  • class DrawableRectangle extends Shape
    protected double w, h public
    DrawableRectangle() w 1.0 h 1.0
    public double area() return w h
    public void draw() ...

Shape
Circle
Rectancle
DrawableCircle
DrawableRectancel
6
How interfaces differ from classes?
  • But this is not a good abstraction we need to
    isolate the notion of a class of drawable
    objects.
  • abstract class Drawable public void draw()
    public void setColour() ...
  • class DrawableRectangle extends Shape, Drawable
    protected double w, h public
    DrawableRectangle() w 1.0 h 1.0
    public double area() return w h
    public draw() ...

7
How interfaces differ from classes?
  • Drawable is a "description of a behavior" we want
    Rectangles and Circles to have.
  • interface Drawable public void draw()
    public void setColour() ...
  • class DrawableRectangle extends Shape
    implements Drawable
  • public void draw()
  • ...

definition of an interface instead of an abstract
class
8
How interfaces differ from classes?
  • A class can implement any number of interfaces
  • class DrawableRectangle extends Shape
    implements Drawable, Cloneable public void
    draw()
  • ...

9
What goes in an interface?
  • Interfaces may contain ordinary abstract methods
  • Interfaces may not contain static methods.
  • interface Sortable // so thispublic boolean
    lessThan (Sortable a)
  • // not thispublic static boolean lessThan
    (Sortable a Sortable b)
  • Interfaces may contain constants which must have
    an implementation
  • interface Drawable
  • ...
  • static final Color DEFAULT_COLOR RED
  • ...

10
More interface syntax
  • Interfaces may be extended and there may be
    multiple extensions.
  • public interface Transformable extends Scalable,
    Rotatable
  • Interfaces can be used as types
  • Drawable items new Drawable5
  • An interface can be declared as a marker
    interface with no methods or constants.
  • The Cloneable interface is used like this. A
    class that implements the interface should have a
    sensible implementation of the clone() method.

11
When to use interfaces?
  • Interfaces should be used whenever you see code
    in the following form.
  • if (x is of type 1)
  • action1(x)
  • else if (x is of type 2)
  • action2(x)
  • We use inheritance when we want to say that a
    class is a certain kind.
  • We use interfaces when we want to say a class has
    a behavior.
Write a Comment
User Comments (0)
About PowerShow.com