oop - How to enforce constructor in Java -
is there way enforce specific constructor in java classes?
for example want classes inherit class have constructor -
public classname(string s1,string s2){....}
i know should not avoided because can lead problems multiple inheritance. there way anyway?
there no facilities in java directly.
however, can enforced extent usin abstract
method.
abstract class base { base(string s1, string s2) { init(s1, s2); } protected abstract void init(string s1, string s2); } class myclass extends base { // forced this. myclass() { super("one", "two"); } // forced this. @override protected void init(string s1, string s2) { } }
Comments
Post a Comment