java - Return subClass -
my question quite easy, cannot figure out how implement want.
i implement method which, depending of given parameter, returns 1 subclass or (i understand have behavior in class create development more object oriented, i'm still learning).
so thought of solution, not compile.
public abstract class a(){ //some code } public class b extends a(){ //some code } public class c extends a(){ //some code } public static void main(string[] args) { system.out.println("input: "); scanner console = new scanner(system.in); string input=console.nextline(); myobject = getobject(input); } public static <? extends a> getobject(string input){ if(input.indexof("b") != -1){ homecoming new b(); } if(input.indexof("c") != -1){ homecoming new c(); } homecoming null; }
first, need remove brackets (()
) class definitions:
public abstract class { //some code } public class b extends { //some code } public class c extends { //some code }
second, getobject
should homecoming a
:
public static getobject(string input){ if(input.indexof("b") != -1){ homecoming new b(); } if(input.indexof("c") != -1){ homecoming new c(); } homecoming null; }
java generics inheritance
No comments:
Post a Comment