Interface constructor generics

Hi,

I’m trying to figure out the syntax of how to tell an interface that certain constructors must be met.

In PHP I just do this:


interface Car {
  public function __construct($stuff, $okay);
}

But what is the equivalency to java? I’ve tried a few different ways but they all fail.


public interface Car { // compilation error: <identifier> expected
  public <E extends Car>(String path);
  public <E extends Car>(String path, int time);
}

public interface Car { // compilation error: <identifier> expected
  public Car(String path);
  public Car(String path, int time);
}

The end result should be that all cars that implement the Car interface must implement the 2 constructors defined by the interface:


public class Viper implements Car {
  public Viper(String path) {
    //etc..
  }

  public Viper(String path, int time) {
    //etc..
  }
}

The only way I got this to work is by using an abstract class instead of an interface. Is this not part of the Java language?

Thanks

hmm I don’t think it is.
It makes sense to me as an Interface describes behaviour. A constructor is more about implementation.
What is the class meant to do with the parameters in the constructor?

Each constructor assigns the correct class attribute. So if the first constructor was used, then the class would only assign the “path” attribute to the class. Likewise, if the 2nd constructor was used, then it would assign the “path” and “time” to the class attribute.

But there isn’t any functionality apart from assigning a few things. That’s why I wanted to do it through an interface because there is no functionality really.

But there isn’t any functionality apart from assigning a few things.

So, there isn’t any functionality defined other than the functionality you’ve defined?

What you want is an abstract class, as detailed here, you can define the constructors in the abstract class, and omit the zero arg constructor, which will cause the programmer to include the constructors you want to require.

lol!

I guess by functionality I meant more of some sort of calculation, if statements, etc. Not assignment, but technically speaking an assignment is a type of functionality.

thanks