开发者

There is no constructor for interface in Java?

In Java, if I have an interface:

public interface MyInterface{
}

开发者_JAVA百科Then MyInterface implementation is:

class MyClass implements MyInterface {
    public MyClass(int a) {
    }
}

So what I mean is that if a user wants to do declare a MyInterface instance with constructor:

MyInterface mine = new MyInterface(2);

then it is not possible right?


MyInterface mine = new MyInterface(2);

then it is not possible right?

That's right. You can never do something like

MyInterface mine = new MyInterface(2);

After new you have to pick a class that implements the interface(*), such as MyClass:

MyInterface mine = new MyClass(2);

Why?
You can think of an interface as a property of a class. An analogy would be an adjective, such as "Red". It makes perfect sense to create, say, a red ball (new RedBall()) or a red car (new RedCar()), but just creating "red" (new Red()) doesn't make sense ("red what??").

(*) You can create anonymous classes that implement the interface on the fly by doing new MyInterface() { ... } but technically speaking you're still instantiating a class.


You are correct. An interface may not specify a constructor, the constructor gets specified in the class that implements the interface.

public interface Foo {
    public void doSomething();
}


public class Bar implements Foo() {
    public Bar(int value) {
    }

    @Override
    public void doSomething() {
    }
}

then when you want to use your interface you need to do something like

Foo foo = new Bar(2);


You can't instantiate an interface directly. You need to use an instance of an implementing class:

myClass mine = new myClassImpl();


It is not possible to define a constructor signature in an java interface.

If you want to assure a construction of objets using a particular signature your options are the following design patterns:

  • Abstract Factory
  • Factory Method


True, this is not possible. What you want to do might be accomplished by following the factory pattern. One thing the code you outlined in your question leaves unspecified is which class implementing your interface should be instantiated at all. You factory implementation is the place where you express your intentions about this in code. The outline for the factory might be something like this, to get you started:

public class FooFactory {
   public myClass createFoo() {
      if (/* some condition */) {
         return new myClassImpl(/* the constructor parameters for that implementation */);
      } else {
         /* return some other implementation; you get the idea */
      }
   }


No it is not possible to declare an instance of myClass since it is an interface.

Interfaces do not define a constructor, but the class implementing the interface still has a constructor.

If you intention is to make it impossible for the user to call the constructor of your class, you can always make the constructor private.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜