I have two classes Parent and Child where child extends parent.
I want to have a method that accepts one parameter that is either the Parent type or Child type.
Is there any method other than using over loading?
Can i write a method that will except any objects i made with classes that extends my parent class or only except parameters that implements a specific interface?
What is the best practice for such situations?
public class Xray{
public void imagine(HumanBase patient)
{ patient.diagnosis = "xray shows no abnormalities"; }
public void imagine(HumanPatient patient)
{ patient.diagnosis = "xray shows no abnormalities"; }
public void imagine(HumanDoctor patient)
{ patient.diagnosis = "xray shows no abnormalities"; }
}
In this example both HumanPatient and HumanDoctor are extends human base.
Since all are human they might need to be xrayed. I had to write multiple methods for each object type.,
But since they all extends same class and impliment similiar interfaces can there be a better way to write such system?
I tried to write a method that only accepts parameter typees that is extended from same parent. I can not find a method other than using over loading.
1