inheritance - Java: Return an object that extends an abstract class which takes arguments -
i've got interface
public interface { public anabstractclass k(); //abstract class constructor takes 2 arguments }
for implementation of interface's method want return generic object extends abstractclass , give 2 argument constructor
to clarify want achieve. in class implements i:
public anabstractclass k() { //return new implementing abstractclass object abstractclassobject(arg1,arg2) }
constructors not inherited, subclasses of anabstractclass
won't have two-argument constructor. if subclass does have two-argument constructor, there nothing stopping creating instance of subclass using two-argument constructor , returning it:
public abstract class anabstractclass { public anabstractclass(string foo, string bar) { system.out.format("created (%s, %s)\n", foo, bar); } } public class baseclass extends anabstractclass { public baseclass(string foo, string bar) { super(foo, bar); } } public interface { public anabstractclass k(); } public class implementation implements { @override public anabstractclass k() { return new baseclass("hello", "world"); } } public class demo { public static void main(string[] args) { i = new implementation(); anabstractclass = i.k(); } }
Comments
Post a Comment