3

I was wondering if it was possible to set a parameter to a method as an Object, which must be a extend one class and implement another. Here is an example: Let's say I have a class called ClassA and an interface called MyInterface.

public class ClassA {
    /* code */
}    


public interface MyInterface {
    /* code */
}

Let's say somewhere else there is a class called ClassB, which both extends ClassA and implements MyInterface.

public class ClassB extends ClassA implements MyInterface {
    /* code */
}

I could also have a ClassC, which also extends ClassA and implements MyInterface:

public class ClassC extends ClassA implements MyInterface {
    /* code */
}

My question is this:

Let's say I have a method called method1, and in method1 I want to have a parameter which accepts an Object. Let's say I wanted this Object to either subclass ClassA or actually be ClassA itself. This is easy to do:

 public void method1(ClassA parameter) {

 }

Let's say I also wanted a method2, and in method2 I want to have a parameter which accepts anything which implements MyInterface. Again, this is easy:

 public void method2(MyInterface parameter) {

 }

But what if I wanted a method3, and I wanted to have a parameter which accepts only objects which either subclass ClassA or is ClassA itself, AND implements MyInterface, and so will accept both ClassB and ClassC, but not any class which only extends ClassA, or only implements MyInterface, or neither. Like:

public void method3 ([Something that extends ClassA implements MyInterface] parameter) {
  /* code */
}
Henry Thompson
  • 2,441
  • 3
  • 23
  • 31
  • You shouldn't. You should figure out the functionality you need, encapsulate that in a type, and require an object of that type (either class or interface.) If either is good enough, then it means they share something in common, and that is what your parameter should capture. – dlev Jul 12 '12 at 18:28
  • Thanks, dlev. You make a very good point- that seems far more sensible, so I will try and do that instead. – Henry Thompson Jul 12 '12 at 22:26

2 Answers2

18

You can do the following:

public <A extends ClassA & MyInterface> void method3(A parameter) { ... }

But I don't think it's a good idea - if you have a concept of object that extends ClassA and implements MyInterface, it would be better to create a separate class or interface to represent that concept.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • That is a great idea. Why didn't I think of that??? Thanks so much! And interesting to see it is actually possible to have such a concept as a parameter, I didn't realise you could do it like that. But you have made a slight mistake... you've written "void" twice, the first one shouldn't be there – Henry Thompson Jul 12 '12 at 22:28
  • 1
    I appreciate the fact that you gave a workable answer to the question, even though you felt a different approach would be wiser. – Erhannis Sep 04 '13 at 11:42
0

From your description, it seems as if you require another type of Object. One that both extends ClassA and implements MyInterface. Perhaps, even, this is an abstract class.

I would then use this new object as the parameter type for method3.

Liggy
  • 1,181
  • 2
  • 11
  • 19