+ 1

mitigate params in polymorphism as child requires diffrent params

I have one interface, implemented in 4 classes.There are some overrided method that do almost same work in every child class. But problem is, there are some params in some methods that are mandatory for some children and unused for some children, It overlaps each other. I have to override method in all children that do not use that param and pass null too in some cases. I want to mitigate those params in such case that no null is passed, all useable params should be passed to overrided method. My code is so complex, I tried to put example simple ``` public interface ParentInterface { void func1(String f1p1, String f1p2); void func2(String f2p1, String f2p2, String f2p3); } public class Child1 implements ParentInterface { @Override public void func1(String f1p1, String f1p2) { System.out.println(f1p1 + f1p2); } @Override public void func2(String f2p1, String f2p2, String f2p3) { System.out.println(f2p1 + f2p3); } } public class Child2 implements ParentInterface { @Override public void func1(String f1p1, String f1p2) { System.out.println(f1p2); } @Override public void func2(String f2p1, String f2p2, String f2p3) { System.out.println(f2p1 + f2p2 + f2p3); } } public static void main(String[] argh) { ParentInterface pi1=new Child1(); pi1.func1("c1f1p1 ","c1f1p2 "); pi1.func2("c1f2p1 ",null , "c1f2p3"); ParentInterface pi2=new Child2(); pi2.func1(null,"c2f1p2 "); pi2.func2("c2f2p1 ", "c2f2p2 ", "c2f2p3"); } ``` Is there any design patttern for this so that mitigation of null values can be possible?

6th Feb 2020, 10:28 AM
Azam
Azam - avatar
3 odpowiedzi
0
if you can declare obj with class type instead of interface type you can do method overloading in class public interface ParentInterface { void func2(String f2p1, String f2p2, String f2p3); } public class Child1 implements ParentInterface { @Override public void func2(String f2p1, String f2p2, String f2p3) { System.out.println(f2p1 + f2p3); } public void func2(String f2p1, String f2p3) { func2( f2p1, null, f2p3 ); } } class Program { public static void main(String[] argh) { //ParentInterface pi1=new Child1(); var pi1=new Child1(); pi1.func2("c1f2p1 ",null , "c1f2p3"); // 1+3 pi1.func2("c1f2p1 ", "c1f2p3"); // 1+3 ParentInterface pi11=new Child1(); ((Child1) pi11) .func2("c1f2p1 ", "c1f2p3"); // possible } } or make implementation of interface but without 'implements' statement it's shorter because it need only one method instead of two
7th Feb 2020, 1:26 AM
zemiak
0
No, this is just example, My work can't be done without polymorphism.
7th Feb 2020, 2:29 AM
Azam
Azam - avatar
0
with reference to your this code, I don't know at run time about children (which it is, as there are 5 children). how can I cast to that specific child. If you say about if else structure then purpose of polymorphism died. ParentInterface pi11=new Child1(); ((Child1) pi11) .func2("c1f2p1 ", "c1f2p3"); // possible It's not possible for me.
7th Feb 2020, 7:05 AM
Azam
Azam - avatar