0

I'm newbie to Java. I would like to ask different between the following interface examples?

public interface MyInterface { public void doSomething(); } 

like this

public class MyClass implements MyInterface { public void doSomething {....} } 

and this

public class MyClass implements MyInterface { protected MyInterface myInterface; public MyClass (MyInterface myInterface) { this.myInterface = myInterface; } public void doSomething () { myInterface.doSomething(); } } 
2
  • 1
    why would you want to do it the second way? How would you create an MyInterface to pass to your constrcutor? What would any benefit be? Commented Aug 8, 2018 at 4:24
  • Interface can have multiple implementations. Therefore it is possible to get different implementation calls at runtime Commented Aug 8, 2018 at 4:25

5 Answers 5

1

In first case you implement an interface using a class and you implement the function doSomething in that class. you can call the doSomething function by creating an instance of the class MyClass

MyInterface obj = new MyClass(); obj.doSomething(); 

In second case, you wont be even able to instantiate an instance of the MyClass because it needs another instance of it-self or another class which implements that interface.

Sign up to request clarification or add additional context in comments.

Comments

0
public class MyClass implements MyInterface { public void doSomething {....} } 

MyClass implements the interface MyInterface. It means your class holds a concrete behavior that your interface promise. By implementing the interface your class guaranteed the MyClass has the concrete feature your interface abstracted in its declaration.

But I doubt you may not have a real scenario to implement an interface as well as create an instance of interface in a class. Second part of your question is one of the most famous design topic of inheritance vs composition. Chances of using both inheritance and composition together of an interface is barely rare.

1 Comment

I'm not native in english, I need more time to unterstand those answer. Thank you very much!
0

The first two code are one interface and another class which implements the interface. The third code is MyClass that implements MyInterface and creates a object reference to MyInterface named myInterface. The next part

 public MyClass (MyInterface myInterface) { this.myInterface = myInterface; } 

is a simple constructor and the next part

public void doSomething () { myInterface.doSomething(); } 

is calling of a method.

Comments

0

The first one is inheritance and the second one is composition. Inheritance is an "is-a" relationship, while composition is a "has-a".

For example, if there is a Pressable interface which represents everything that can be pressed, Button, Checkbox should implement it. If there is a Color class, the Button should have a composition relationship between the Color, since a Button should have a color, but a Button is not a type of Color.

A commonly known mistake is the java.util.Stack. Since a Stack is not a java.util.Vector, Stack should not inherit Vector.

Comments

0

An interface is an abstract type in Java and it specify a set of abstract methods that classes must implement. A class usually implement the interface as shown in your first example.

In your second example, even though MyClass is implementing the interface, the behaviour of doSomething method will depend on the instance of MyInterface implementation that it will get when instantiate MyClass object.

It is not possible to instantiate an interface. You will have to do something like below. Here MySecondClass implements the MyInterface.

 MyClass m = new MyClass(new MyInterface() { @Override public void doSomething() { // TODO Auto-generated method stub } }); MyClass m2 = new MyClass(new MySecondClass()); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.