Interfaces in Java - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Interfaces in Java

Description:

Interfaces in Java – PowerPoint PPT presentation

Number of Views:624
Avg rating:3.0/5.0
Slides: 35
Provided by: PerL57
Category:

less

Transcript and Presenter's Notes

Title: Interfaces in Java


1
Interfaces in Java
2
What is an interface?
  • public class BankAccount
  • public BankAccount() ...
  • public void deposit(double amount) ...
  • public void withdraw(double amount) ...
  • public double getBalance() ...

3
What is an interface?
  • If we have agreed on the interface
  • but do not have any sensible default
    implementation of the interface
  • then why must we provide a (perhaps useless)
    class with a default implemen-tation to the user?

4
What is an interface?
  • public class Shape
  • public void draw() ???
  • public double getArea() ???
  • We can use interfaces instead!

5
What is an interface?
  • public interface Shape
  • void draw()
  • double getArea()

New keyword!
6
Interface properties
  • An interface is different than a class
  • All methods are abstract they have no
    implementation
  • All methods are automatically public
  • No instance fields

7
Why use interfaces?
  • Promotes loose coupling!
  • Another programmer working with shape classes
    need only to know the interface type, not
    concrete shape classes
  • Program to an interface, not an implementation

8
Why use interfaces?
Shape
draw() getArea()
9
Implementing interfaces
  • Any class that wishes to implement a given
    interface, must explicitly state this
  • Not enough just to implement the methods
    themselves
  • For this purpose, we use the implements keyword

10
Implementing an interface
  • public class Circle implements Shape
  • private double radius
  • private double x
  • private double y
  • public Circle(...) ...
  • public void draw() ... // from Shape
  • public double getArea() ... // from Shape
  • public double getRadius() ... // New

11
Programming with interfaces
  • Now, only the creator of various shape objects
    need to know the exact type of a concrete shape
  • Other parts of the code only need to know the
    interface
  • The latter code is loosely coupled to the
    concrete shape classes

12
Programming with interfaces
  • public void produceShapes()
  • Shape s1 new Circle(80,300,300)
  • Shape s2 new Square(50,200,200)
  • Shape s3 new Point(100,400)
  • processShape(s1)
  • processShape(s2)
  • processShape(s3)

13
Programming with interfaces
  • public void processShape(Shape s)
  • double area s.getArea()
  • String result Area is area
  • System.out.println(result)
  • s.draw()

14
Important!!
  • This is not possible
  • Shape s new Shape() // NO!
  • We can never create an object that (only) has an
    interface type
  • A variable can have the type reference to an
    interface

15
Important!!
  • In other words
  • Shape s1 new Square(10,10,10) // OK
  • Shape s2 new Point(20,20) // OK
  • Circle c new Circle(5,10,20) // OK
  • Shape s new Shape() // NO!
  • Circle c new Shape() // NO!
  • Square sq new Circle(5,20,40) // NO!

16
Conversions
  • Why does this actually work?
  • Shape s new Circle(10,10,10)
  • The object we create has type Circle
  • The variable has the type Shape
  • The class Circle implements the interface Shape
  • This makes the conversion from Circle to Shape
    legal

17
Conversions
  • You can always convert from a class type to an
    interface type, provided the class implements the
    interface

Shape
Circle
18
Conversions
  • There is a price to pay
  • We can now only use methods defined in the Shape
    interface on the object
  • Shape s new Circle(10,10,10)
  • double r s.getRadius() // NO!

19
Conversions
  • How about this?
  • public void enlarge(Shape s)
  • Circle c s
  • double r c.getRadius()
  • c.setRadius(2r)

Error!
20
Conversions
  • How about this then?
  • public void enlarge(Shape s)
  • Circle c (Circle)s
  • double r c.getRadius()
  • c.setRadius(2r)

OK if s is a Circle!
21
Conversions
  • You can convert from an interface type to a
    class type, but only if the actual classes are
    the same being sure of that is your
    responsibility!

Shape
Circle
Square
22
Using interfaces for callbacks
  • Consider a simple task comparing two objects,
    to see which one is best
  • Easyif we know what we mean by best

23
Using interfaces for callbacks
  • For numbers, this is a very simple task
  • public bool AisBest(int a, int b)
  • return (a gt b)

24
Using interfaces for callbacks
  • For a BankAccount class, perhaps like this
  • public bool AisBest(BankAccount a, BankAccount
    b)
  • return (a.getBalance() gt b.getBalance())

25
Using interfaces for callbacks
  • Major problem We have to write a new method for
    each type
  • Maybe we can use interfaces to help us!
  • public interface Measurable
  • double getMeasure()

26
Using interfaces for callbacks
  • public bool AisBest(Measurable a, Measurable b)
  • return (a.getMeasure() gt b.getMeasure())
  • This will work for all classes implementing the
    Measurable interface!

27
Using interfaces for callbacks
  • This is great, but
  • we can only make our own classes implement the
    Measurable interface
  • We cannot force e.g. the Rectangle class to
    implement the Measurable interface

28
Using interfaces for callbacks
  • We would like something like this
  • public bool AisBest(Object a, Object b)
  • return (a.getMeasure() gt b.getMeasure())
  • but Object does not implement the Measurable
    interface (of course)

29
Using interfaces for callbacks
  • A different approach is to create an inter-face
    for making a measurement, not giving a
    measurement
  • Class is then no longer responsible for measuring
    itself
  • A bit contrary to OO-philosophy

30
Using interfaces for callbacks
  • Define an interface for being able to make a
    measurement
  • public interface Measurer
  • double measure(Object x)

31
Using interfaces for callbacks
  • Define a class being able to measure a Rectangle
    object
  • public class RectMeasurer implements Measurer
  • public double measure(Object x)
  • Rectangle rect (Rectangle)x
  • // Code for appropriate measure

32
Using interfaces for callbacks
  • We then get
  • public bool AisBest(Object a, Object b, Measurer
    m)
  • return (m.measure(a) gt m.measure(b))

33
Using interfaces for callbacks
  • We call it like this
  • Measurer m new RectMeasurer()
  • Rectangle r1 new Rectangle(10,20,30,40)
  • Rectangle r2 new Rectangle(15,25,35,45)
  • boolean aIsBetterThanB AisBest(r1,r2,m)

34
Using interfaces for callbacks
  • Advantages
  • Can compare all classes
  • Can change strategy for measurement, without
    changing class itself
  • Drawbacks
  • A bit risky danger of mixing up objects and
    measurers (do we like casting?)
  • Somewhat anti-OO
Write a Comment
User Comments (0)
About PowerShow.com