-1

I have created an interface Response

Interface Response{
   public output(String s);
}

and i implement this interface on my class A . now i have a class B too and it also need to implement this interface but it need different parameter for interface method. (mean class B needs public output(Comment c);)

So my question is , do i need to create another interface for class B because it need different parameter

Like this

 Interface Response{
   public output(Comment c);
}

or is there any way to handle this. also can i used object data type for this.?

Thank you.

Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
  • Are you interacting with the class `B` through its interfaces or through its own methods? – Sotirios Delimanolis Mar 25 '14 at 04:25
  • Did you intend to tag this as both C# and Java? – Dawood ibn Kareem Mar 25 '14 at 04:27
  • i think this is common for both C# and java. – Waqar Ahmed Mar 25 '14 at 04:28
  • basically i am trying to do this .(the 1st answer on this page). He create interface for only String data type but i also need this interface for other datatypes as well. http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a – Waqar Ahmed Mar 25 '14 at 04:30
  • Maybe, but many of the people who come here will be expert in EITHER Java OR C#, and not know whether their answer applies to both. So in its current form, this question should only be answered by people who know both languages. Also, someone might want to include a code snippet in their answer - what are they to do? You should specify which language you actually care about, whether you believe the answer is "common to both" or not. – Dawood ibn Kareem Mar 25 '14 at 04:31
  • i removed the C# tag. – Waqar Ahmed Mar 25 '14 at 04:32
  • I suspect what you actually want is a generic. I could explain to you how they work in Java, but I don't know how C# generics work. – Dawood ibn Kareem Mar 25 '14 at 04:32
  • explain for java please and thanks for your kind help. – Waqar Ahmed Mar 25 '14 at 04:33
  • basically i need this in my android code. – Waqar Ahmed Mar 25 '14 at 04:34

1 Answers1

3

Note, this answer applies to Java. I am not a C# expert.

I believe you want

public interface Response<T> {
    void output(T arg);
}

Then you can write

public class ClassA implements Response<String> {
    public void output(String arg) {
       // etc

and also

public class ClassB implements Response<Comment>
    public void output(Comment arg) {
       // etc
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110