interface parentInterface
{
public String methodA(/*define parameters name and dataType*/);
}
and
public class childA : parentInterface
{
public String methodA(String a, int b, String c, long d){}
}
public class childB : parentInterface
{
public String methodA(int e, String f, String g){}
}
I want to define interface method's parameters name and data type
Make a New Parameter
This can often be solved by using a class
or struct
to use as single parameter rather than the built-in Types.
The Interface
You know what to expect from a class
when it implements a familiar interface
. We know that all classes implementing the IEnumerable
interface can be used in a foreach
loop. By convention, the name of the interface is "I" followed by a description of an ability. It is typical for the name to end with the suffix "-able".
-able Suffix forming adjectives meaning:
1 -able to be [as in] calculable.
2 -having the quality of [as in] comfortable.
Oxford English Dictionary
Let's rename parentInterface
and MethodA()
to give a clear example of how this normally works (and to avoid negative sanctions):
public interface ITreatable
{
Treatment GetTreatment();
}
Well, finding the cure may not be so easy, even if the object
represents a treatable illness. Here's some examples:
public class TheFlu : ITreatable
{
public Treatment GetTreatment(int year)
{
// return some object, Treatment, based on the flu season.
}
}
public class Hangover : ITreatable
{
public Treatment GetTreatment()
{
return Treatment.Empty; // no parameters necessary.
}
}
public class Insomnia : ITreatable
{
public Treatment GetTreatment(FamilyHistory occurances, LabResult lab)
{
// return Some Treatment object that can be different based on the
// calculated risk from the arguments.
}
}
What We're Really Missing Here
I don't know biology, but the concept is still the same. You have a group of ITreatable
illness objects that need to have a GetTreatment()
method; however, they use different criteria for making calculations. We need Symptoms
.
public class Symptoms
{
public FamilyHistory History;
public DateTime Time;
public LabResult Lab;
public BloodTest BloodTest;
public TimeSpan SymptomTime;
public IsCritical IsCritical;
}
Now, the objects can parse the symptoms in their own method, and our interface will look like this:
public interface ITreatable
{
Treatment GetTreatment(Symptoms symptoms);
}
You have two different methods
public String methodA(String a, int b, String c, long d){}
and
public String methodA(int e, String f, String g){}
that represent two different contracts to childA and childB respectively. You cannot define an interface with a single methodA
that fits both definitions. What you seek to do is not possible.
Note that you could define both overloads in your interface, but then each class implementing that interface would have to implement both overloads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With