java - How to use inheritance correctly and avoid downcasting -


let's set hypothetical scenario. have parent class

public class {     protected void dosomething(){         //do way     }  } 

and child class

public class b extends a{     @override     protected void dosomething(){         // b way     }  } 

and have hypothetical class that:

public class someclass implements someinterface{     @override     public void somemethod(a aobject){         aobject.dosomething();     }  } 

but want use behavior of b, can't change method parameter nor can downcast b.

obviously i'm handling bigger problem same principle, answer have myself clone properties of b, use b. before i'd hear more opinions.

edit: constraint, can't pass instance of b method nor use instanceof method.

edit 2: never receive instance of b because someclass overrides method interface must use instance of a.

edit 3: situation generated poorly designed legacy code ran into, wanted figure out faster clean way fix excercise.

just pass instance of b somemethod() since b "isa" a, work , call b's dosomething().


Comments