10

I'm pretty much java beginner, and i've come to ask stackoverflow forum for one simple question. I already checked similar question here on SO, but i didn't found the desired answer, LINK: Is there any relation between the class that implements interface and that interface?

We know if Cat extends Animal, there is a relationship here. So Cat is-a Animal ( but not necessarily otherwise. Animal is also a Dog..). We know this relationship as "is-a" - inheritance.

If we have another two classes: Girl and Candy and if class Girl holds instance of Candy, then we name this relationship "has-a"-composition, because we can say that Girls has-a Candy.

Correct me if i made something wrong with above examples.

Anyway my question is if its there some relationship between objects of classes that implement the same interface? Let's say:

public class A implements InterfaceA {}

public class B implements InterfaceA {}

If there is some relationship, how we can make use of that relationship? Thanks for your answers in advance..

EDIT: For those who think that there isn't a relationship, i have a book "java how to program ninth edition" and there is a UML diagram which shows classes that implements same interface in "relationship" ?

Image:

enter image description here

Community
  • 1
  • 1
rootpanthera
  • 2,731
  • 10
  • 33
  • 65
  • A is a InterfaceA and we can check it by `myObject instanceof InterfaceA`, where myObject is instance of A or B – hoaz Nov 19 '12 at 16:28
  • I suggest you read about [Composition over inheritance](http://en.wikipedia.org/wiki/Composition_over_inheritance) – Christophe Roussy Nov 19 '12 at 16:33
  • I am surprised by how fundamentally most answerers have misunderstood this question. It seems quite clear to me. – Tom Anderson Nov 19 '12 at 16:59

4 Answers4

6

There is no relationship between the objects which implement same interface. Both are independent implementations of their own. They just follow the protocol defined by interface.

If you try to cast from one object type to another object type, you will end-up getting ClassCastException.

EDIT:

Your UML diagram suggest that there is relationship between interface and class (IS-A), but not between the two classes which implement same interface. There is no line with relationship symbol between the classes (like you see between class and interface)

kosa
  • 65,990
  • 13
  • 130
  • 167
3

This means that your classes have similar behaviour described by the interface methods.

For example, consider two classes: Fish and Bird. They both can implement interface Moveable like:

inteface Moveable{
    void move(int x, int y, int z);
}

class Fish implements Moveable{
    public void move(int x, int y, int z){
       swim(x, y, z); 
   }
    private void swim(int x, int y, int z){
      //a method describing how the fish swims
    }
}

class Bird implements Moveable{
    public void move(int x, int y, int z){
       fly(x, y, z); 
   }

    private void fly(int x, int y, int z){
      //a method describing how the bird flies
    }
}

You can call the behaviour specified by interface methods like:

Moveable bird = new Bird();
Moveable fish = new Fish();

bird.move(10,10,10);
fish.move(10,10,-10);

So either of them can move, though the way they move differs greatly. This way both classes share common behaviour though its implementation may be unknown to you.

svz
  • 4,516
  • 11
  • 40
  • 66
0

The relationship between a class implementing and interface, and the interface is also an "is-a" relationship, the same as extending from a class.

It's useful (or at least one reason it's useful) because by providing an interface, you can code to an interface rather than code to an implementation of that interface, making it easy (if you ever need to) to swap your implementations over with the minimum of fuss.

Note that if you're going "across" the inheritance hierarchy, so looking at the relationship between two classes which inherit the same interface, there is no implicit relationship here at all.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • The relationship between a class implementing an interface and that interface is best described as an IS association. As in: an Employee IS (implements) Fireable. – gherson Mar 12 '17 at 17:47
0

There is no formal name for the relationship between two classes which implement the same interface.

Or, for that matter, which extend the same base class. If a child which extends another is its child, then two children of the same class are siblings, but this is not a term which is generally used.

If you wanted to extend this anthropomorphic metaphor to interfaces, you could call two implementations of an interface look-alikes, perhaps.

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133