Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationships between classes that implement same interface

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

like image 973
rootpanthera Avatar asked Nov 19 '12 16:11

rootpanthera


People also ask

What happens when two classes implement the same interface?

A class can implement many interfaces but can have only one superclass. An interface is not part of the class hierarchy. Unrelated classes can implement the same interface.

What is a relationship between a classes and interface?

RELATIONSHIP BETWEEN CLASS AND INTERFACEA class can extend another class, an interface can extend another interface, but a class implements an interface.

Can multiple classes can implement the same interface?

A class can implement multiple interfaces and many classes can implement the same interface. Final method can't be overridden.

What are the relationships between classes?

Classes can be related in two ways: An aggregation relation, named Has-a: class C2 is related by Has-a with class C1 when C2 has a field whose type is that of class C1. This relation can be generalized as: C2 has at least one field whose type is that of class C1.

What is the relationship between classes and interfaces in Java?

A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

When a class A implements an interface B What type of relationship created between them?

We know this relationship as "is-a" - inheritance.


2 Answers

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)

like image 118
kosa Avatar answered Oct 16 '22 22:10

kosa


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.

like image 39
svz Avatar answered Oct 16 '22 20:10

svz