Relationships between classes that implement same interface

15,462

Solution 1

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)

Solution 2

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.

Share:
15,462
rootpanthera
Author by

rootpanthera

Updated on July 20, 2022

Comments

  • rootpanthera
    rootpanthera almost 2 years

    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

  • kosa
    kosa over 11 years
    @rootpanthera: Glad it helped. Enjoy coding.
  • Viral Sarvaiya
    Viral Sarvaiya almost 11 years
    Thanks for saving life. indirectly i get point where i am wrong. :)
  • gherson
    gherson over 7 years
    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.