Reputation: 51
I have trouble understanding when multiple classes implements same interface, before i was using concrete classes as DI but after searching and reading i was told that it should depend on abstaraction.So this is my problem:
interface SomeInterface{
public void doSomething();
}
We have two classes that implements interface:
class FirstClass implements SomeInterface{
@Override
public void doSomething(){
System.out.print("First class is doing something");
}
}
class SecondClass implements SomeInterface {
@Override
public void doSomething() {
System.out.print("Second class is doing something");
}
}
And now i have a client where i want to use method from First and Second class:
class ClientClass {
private SomeInterface someInterface;
public ClientClass(SomeInterface someInterface) {
this.someInterface = someInterface;
}
public void testMethod() {
this.someInterface.doSomething();
}
}
Now when i instantiate Client class i can only pass one class in constructor either first or second, and that method will start, but what if i want both method? What is wrong with this design?
Upvotes: 2
Views: 2841
Reputation: 40024
At least two of the advantages of an interface is to 1) provide abstract methods that could be implemented by more than one class to tie together
common aspects of the implementing classes, and 2) to allow a mechanism by which those interfaces and others may be implemented by the same class (as opposed to a single inheritance model).
If your different classes have the same meaning and and return type I see no reason that the same interface can't be implemented by all. Other interfaces can also be implemented (and interfaces may extend other interfaces).
If each class uses the same interface differently then it is highly advisable to create a new interface as needed. Otherwise your code would be very confusing to future maintainers. It could also inadvertently lead to problems if used as described below (which is typical for shared interfaces).
But imagine a list of five different classes that require a getTotal()
method. Declare an interface named Results
and have those classes implement it. Then every instance could be placed in a List<Results>
which could then iterated with each reference invoking the getTotal()
method.
Upvotes: 1
Reputation: 726479
You can use both methods, just not at the same time (unless you build another implementation that lets you make composite implementations).
For example, you can have different instances of ClientClass
using different implementations of SomeInterface
:
ClientClass clA = new ClientClass(new FirstClass());
ClientClass clB = new ClientClass(new SecondClass());
Now you have two objects, clA
and clB
, which have the same type, but different behavior, because they enclose a different instance of SomeInterface
.
When you add DI (dependency injection) to the picture, you can write the code that makes ClientClass
instances in a way that it has no knowledge of FirstClass
or SecondClass
, which lets you keep the client implementations independent of the SomeInterface
hierarchy.
Upvotes: 1