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();
}
}
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.
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
.
A commonly known mistake is the java.util.Stack
. Since a Stack
is not a java.util.Vector
, Stack
should not inherit Vector
.
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.
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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.