Pergunta

I've just started Java, and are going through examples online at: http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html

Then I got to implementing an interface, while I understand the concept behind it, but it seems weird to me, as in the interface declaration you only defines it, and in the class that implements this interface you'll still have to write it's functions. So why use it at all?

I tried the sample code, and then change the code to remove the interface, and they both work the same. So my question is when will implementing interface be used? It looks unnecessary to me. Thanks in advance!

Sample Code online:

public class RectanglePlus 
    implements Relatable {
    public int width = 0;
public int height = 0;
public Point origin;

// four constructors
public RectanglePlus() {
    origin = new Point(0, 0);
}
public RectanglePlus(Point p) {
    origin = p;
}
public RectanglePlus(int w, int h) {
    origin = new Point(0, 0);
    width = w;
    height = h;
}
public RectanglePlus(Point p, int w, int h) {
    origin = p;
    width = w;
    height = h;
}

// a method for moving the rectangle
public void move(int x, int y) {
    origin.x = x;
    origin.y = y;
}

// a method for computing
// the area of the rectangle
public int getArea() {
    return width * height;
}

// a method required to implement
// the Relatable interface
public int isLargerThan(Relatable other) {
    RectanglePlus otherRect 
        = (RectanglePlus)other;
    if (this.getArea() < otherRect.getArea())
        return -1;
    else if (this.getArea() > otherRect.getArea())
        return 1;
    else
        return 0;               
}
}

Code that I changed to, taken out the interface, it still works the same

public class RectanglePlus {
public int width = 0;
public int height = 0;
public Point origin;

// four constructors
public RectanglePlus() {
    origin = new Point(0, 0);
}
public RectanglePlus(Point p) {
    origin = p;
}
public RectanglePlus(int w, int h) {
    origin = new Point(0, 0);
    width = w;
    height = h;
}
public RectanglePlus(Point p, int w, int h) {
    origin = p;
    width = w;
    height = h;
}

// a method for moving the rectangle
public void move(int x, int y) {
    origin.x = x;
    origin.y = y;
}

// a method for computing
// the area of the rectangle
public int getArea() {
    return width * height;
}

// a method required to implement
// the Relatable interface
public int isLargerThan(RectanglePlus otherRect) {

    if (this.getArea() < otherRect.getArea())
        return -1;
    else if (this.getArea() > otherRect.getArea())
        return 1;
    else
        return 0;               
}

public static void main( String[] args )
{

    RectanglePlus newRect = new RectanglePlus(20, 30);
    RectanglePlus somerect = new RectanglePlus(50, 100);

    System.out.println("Area of newRect is " + newRect.getArea());

    System.out.println("Area of somerect is " + somerect.getArea());

    if((newRect.isLargerThan(somerect))==1)
    {
        System.out.println("newRect is bigger");
    }
    else
    {
        System.out.println("somerect is bigger");
    }

}

}
Foi útil?

Solução

Two reasons:

  1. If you will have more than one implementation of the interface. Say you have Shape and subtypes is Rectangle, Oval, etc. If you want to write code that can do something with shapes in general, you need an interface which all of the subtypes implement - the interface is the set of methods you know that any Shape will have.

  2. If you are writing an API - you are writing a library which someone else will use. You provide other people with interfaces - this is the stuff that it's okay for them to call. You will implement the interface, and the implementation class may have more methods - and you would like to be able to change those methods later, but your users should be able to pick up the new version of your library and use it with their old code. By separating the interface from the implementation, you give the public something they can use, but preserve for yourself something you can change without hurting existing users.

Outras dicas

This is to facilitate type/interface reuse. You can pass a sub-type's object where a parent type object is expected. You can refer to http://www.oodesign.com/liskov-s-substitution-principle.html.

This basically allows you to deal in an abstract way. Your program can handle objects of different Classes as long as they implement certain behavior (or implement an interface or extend from a class)

If you implements Relatable it allows you to find the largest object in a pair of objects, for any objects that are instantiated from a class that implements Relatable. Otherwise you only can find the largest object in a pair of objects that are instantiated from the same class.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top