Java interface implementation?
Clash Royale CLAN TAG#URR8PPP
Java interface implementation?
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();
MyInterface
Interface can have multiple implementations. Therefore it is possible to get different implementation calls at runtime
– Jude Niroshan
Aug 8 at 4:25
5 Answers
5
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.
I'm not native in english, I need more time to unterstand those answer. Thank you very much!
– edzhome
Aug 8 at 4:44
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
doSomething
doSomething
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.
MyClass
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.
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
.
Pressable
Button
Checkbox
Color
Button
Color
Button
Button
Color
A commonly known mistake is the java.util.Stack
. Since a Stack
is not a java.util.Vector
, Stack
should not inherit Vector
.
java.util.Stack
Stack
java.util.Vector
Stack
Vector
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.
MyClass
doSomething
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());
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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?– Scary Wombat
Aug 8 at 4:24