There is no constructor for interface in Java?

前端 未结 6 549
深忆病人
深忆病人 2020-12-17 06:48

In Java, if I have an interface:

public interface MyInterface{
}

Then MyInterface implementation is:

class MyC         


        
相关标签:
6条回答
  • 2020-12-17 07:06
    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.

    0 讨论(0)
  • 2020-12-17 07:07

    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
    0 讨论(0)
  • 2020-12-17 07:17

    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 讨论(0)
  • 2020-12-17 07:19

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

    myClass mine = new myClassImpl();
    
    0 讨论(0)
  • 2020-12-17 07:24

    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 */
          }
       }
    
    0 讨论(0)
  • 2020-12-17 07:29

    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);
    
    0 讨论(0)
提交回复
热议问题