2

In Java, if I have an interface:

public interface MyInterface{
}

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?

aioobe
  • 413,195
  • 112
  • 811
  • 826
olidev
  • 20,058
  • 51
  • 133
  • 197
  • 2
    your class should be like this: `class myClassImpl implements myClass`. Also read this: http://download.oracle.com/javase/tutorial/java/concepts/interface.html – Harry Joy Jun 01 '11 at 12:59

6 Answers6

19
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.

aioobe
  • 413,195
  • 112
  • 811
  • 826
4

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);
JohnnyO
  • 3,018
  • 18
  • 30
  • Hi, this looks nice. But if I have something like this: I Cannot instantiate the type Foo like this: Foo foo = new Foo()? – olidev Jun 01 '11 at 13:13
  • 1
    No, because Foo is an interface, it cannot be instantiated. Only classes can be instantiated – JohnnyO Jun 01 '11 at 13:51
3

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

myClass mine = new myClassImpl();
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

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:

BertNase
  • 2,374
  • 20
  • 24
0

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 */
      }
   }
Waldheinz
  • 10,399
  • 3
  • 31
  • 61
0

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.

Eystein Bye
  • 5,016
  • 2
  • 20
  • 18