inheritance - How do I implement abstract class inside abstract class in the implementation class in Java? -
i have 2 classes have structures following code.
public class classone { private data data; public classone(data data) { this.data = data; } public data getdata() { return data; } public static class data { //code 1 } }
public class classtwo { private data data; public classtwo(data data) { this.data = data; } public data getdata() { return data; } public static class data { //code 2 } }
as can see, 2 classes have data
object has different implementation. move data
object in parent class , implement in 2 classes? might looks following code.
public abstract class abstractclass { data data; public abstractclass(data data) { this.data = data; } public data getdata() { return data; } public abstract class data{} }
public class classone extends abstractclass { public classone(data data) { super(data); } //implementation abstractclass.data public class data { //code 1 } }
public class classtwo extends abstractclass { public classtwo(data data) { super(data); } //implementation abstractclass.data public class data { //code 2 } }
as know, java extends
1 class, how implement inner abstract class (data
)?
this do, if understand problem correctly.
public class abstractclass { protected abstractdata data; public abstractclass(abstractdata data) { this.data=data; } public abstractdata getdata() { return data; } public static interface abstractdata { public int getint(); public boolean getbool(); } } public class classone extends abstractclass { public classone(abstractdata data) { super(data); } @override public abstractdata getdata() { return data.getbool() ? data : null; } public static class dataone implements abstractdata { @override public int getint() { return 0; } @override public boolean getbool() { return false; } } } public class datatwo implements abstractclass.abstractdata { @override public int getint() { return 42; } @override public boolean getbool() { return true; } }
here can combine abstractclass
, classone
both dataone
, datatwo
like.
but don't see why abstractclass
should abstract, isn't in exmple. of course, can make abstract , add abstract method like.
Comments
Post a Comment