5

I want to force some classes to implement constructor that initialize the object from Map. Is there a way to do it like interfaces make it for regular methods?

public class SomeClass implements SomeInterface {
    public SomeClass(Map data) {
        // init object
    }
}

For example, I want all classes that implements SomeInterface to be forced to have constructor with one parameter Map. Is there any way to do it?

micobg
  • 1,272
  • 6
  • 21
  • 35
  • 2
    https://stackoverflow.com/questions/3164334/how-can-i-force-a-constructor-to-be-defined-in-all-subclass-of-my-abstract-class – ayush Aug 09 '17 at 13:09
  • as far as I know, the only way purposely call a constructor when you have multiple constructors in one class is to make the parameters unique for each constructor – ja08prat Aug 09 '17 at 13:10
  • 1
    There is no way to do this so that the compiler would give an error if the class does not implement the specific constructor. Why would you want to enforce this? It's normally the class' own business how it initializes itself, and requiring something like this is not necessary. – Jesper Aug 09 '17 at 13:10
  • hide all the others constructors... – ΦXocę 웃 Пepeúpa ツ Aug 09 '17 at 13:11
  • You can't do it because you may have other constructors too. If you do not have any other constructor, this will force the consumers to give a map.If you force to send a map to this constructor, they can bypass with an empty map. – Suresh Atta Aug 09 '17 at 13:16
  • There is no problem to have other constructors. I just wan to be sure that this constructor is available. I'm searching for something like interface that can be used not just for methods but for constructors too. – micobg Aug 09 '17 at 13:26

0 Answers0