Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces in Java.

Similar presentations


Presentation on theme: "Interfaces in Java."— Presentation transcript:

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() {...} } DCS – SWC

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? DCS – SWC

4 What is an interface? We can use interfaces instead!
public class Shape { public void draw() {???} public double getArea() {???} } We can use interfaces instead! DCS – SWC

5 What is an interface? public interface Shape { void draw();
double getArea(); } New keyword! DCS – SWC

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 DCS – SWC

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”… DCS – SWC

8 Why use interfaces? Shape + draw() + getArea() Circle - radius - x - y
+ getRadius() Square - length + Square(…) Point + Point(…) DCS – SWC

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 DCS – SWC

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 } DCS – SWC

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 DCS – SWC

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); } DCS – SWC

13 Programming with interfaces
public void processShape(Shape s) { double area = s.getArea(); String result = ”Area is ” + area; System.out.println(result); s.draw(); } DCS – SWC

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” DCS – SWC

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! DCS – SWC

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 DCS – SWC

17 Conversions ”You can always convert from a class type to an interface type, provided the class implements the interface” Shape Circle DCS – SWC

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! DCS – SWC

19 Conversions How about this…? public void enlarge(Shape s) {
Circle c = s; double r = c.getRadius(); c.setRadius(2*r); } Error! DCS – SWC

20 Conversions How about this then…? public void enlarge(Shape s) {
Circle c = (Circle)s; double r = c.getRadius(); c.setRadius(2*r); } OK – if s is a Circle! DCS – SWC

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 Square Circle DCS – SWC

22 Using interfaces for callbacks
Consider a simple task – comparing two objects, to see which one is ”best” Easy…if we know what we mean by ”best” DCS – SWC

23 Using interfaces for callbacks
For numbers, this is a very simple task public bool AisBest(int a, int b) { return (a > b); } DCS – SWC

24 Using interfaces for callbacks
For a BankAccount class, perhaps like this: public bool AisBest(BankAccount a, BankAccount b) { return (a.getBalance() > b.getBalance()); } DCS – SWC

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(); } DCS – SWC

26 Using interfaces for callbacks
public bool AisBest(Measurable a, Measurable b) { return (a.getMeasure() > b.getMeasure()); } This will work for all classes implementing the Measurable interface! DCS – SWC

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 DCS – SWC

28 Using interfaces for callbacks
We would like something like this… public bool AisBest(Object a, Object b) { return (a.getMeasure() > b.getMeasure()); } … but Object does not implement the Measurable interface (of course) DCS – SWC

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… DCS – SWC

30 Using interfaces for callbacks
Define an interface for being able to make a measurement: public interface Measurer { double measure(Object x); } DCS – SWC

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 } DCS – SWC

32 Using interfaces for callbacks
We then get… public bool AisBest(Object a, Object b, Measurer m) { return (m.measure(a) > m.measure(b)); } DCS – SWC

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); DCS – SWC

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… DCS – SWC


Download ppt "Interfaces in Java."

Similar presentations


Ads by Google