0

I have an abstract class which implements two interfaces. Am I right in thinking Because I use two interfaces, I cannot use either interface to implement dynamic binding? Reason being if I were to use one of the interfaces, I would obviously not be able to invoke methods from the other interface as the type system would only allow the sub type to invoke methods defined by the interface I used to declare the polymorphic variable?

Therefore, my actual question is it ok that I am only really using the interfaces to ensure my abstract class (or the subclasses) definitely provide an implementation for the methods? This seems to contradict what Item 19 states- you should only use interfaces for types (I took that to mean polymorphism).

Example:

public interface A{
    public void meth1();
}

public interface B{
    public void meth2();
}

public abstract class C implements A,B{

}

public void DynamicBinding(A aobject){
  //Can only call aobject.meth1();
}
user997112
  • 29,025
  • 43
  • 182
  • 361
  • 4
    I don't understand your question. Dynamic binding is very closely related to polymorphism. In fact, I think they are two terms for almost the same thing. – Code-Apprentice Jul 28 '12 at 22:28
  • What I am saying is, I am unable to use the interfaces for subtyping because I need to use methods from both interfaces in the polymorphic variable. Therefore is it ok to use the abstract class for subtyping and then the interfaces are only used to make sure methods are implemented? This seems contradictory to item 19 – user997112 Jul 28 '12 at 22:35

3 Answers3

5

You can use generics to have your method take a parameter of both type A and B:

public <T extends A & B> void method(T param) {
  param.meth1(); // fine
  param.meth2(); // also fine
}

Related question here

Community
  • 1
  • 1
Jorn
  • 20,612
  • 18
  • 79
  • 126
  • Wouldn't this have the exact same effect as passing Code-Guru's subinterface C as type parameter for the method? – Håvard Geithus Jul 28 '12 at 22:46
  • 1
    For this one example, yes. There are some other benefits to this solution though, such as you don't have to create a subinterface, and it will also work if the first parameter is a class (instead of interface). – Jorn Jul 28 '12 at 22:49
  • So if it was a class, would "extends" be interpreted as "implements"? (Java doesn't allow for multiple inheritance). – Håvard Geithus Jul 28 '12 at 22:51
  • No, for the `` syntax, both on methods and classes, it's always extends. They're basically the same thing anyway. – Jorn Jul 28 '12 at 22:53
1

When you need methods from only A then A can be used as an object's type as you have illustrated. Similarly for B. If you need methods from both, you make make a new interface:

public interface C extends A, B {
}

Interfaces are allowed to extend more than one interface.

Then you can add an abstract class with default implementations, if you wish:

public abstract class D implements C {
  // implementation details
}
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • What about my abstract class though- this already contains the methods for both? Thats why I thought it would be ok to use the abstract class for subtyping? – user997112 Jul 28 '12 at 22:45
  • Can you include the abstract class into the question so we can see your suggestion. – Håvard Geithus Jul 28 '12 at 22:49
  • @HåvardGeithus I accidently forgot to include "abstract" for the definition of class C. It is there now... – user997112 Jul 28 '12 at 22:51
  • @HåvardGeithus The interface in my example removes the need for an abstract class, unless you want to extend the example further to provide one with a default implementation. That's another issue. – Code-Apprentice Jul 28 '12 at 22:52
  • @user997112 If you want to follow the suggestion of programming to the interface, you should do as I suggested in my answer. From there, you can provide a default implementation with an abstract class (call it `D`) if you wish. You would still use the interface `C` as the type for parameters and variables. – Code-Apprentice Jul 28 '12 at 22:54
  • Yes, I know. I was only confused when the original poster referred to some abstract class that wasn't present in the question :) – Håvard Geithus Jul 28 '12 at 22:54
  • @HåvardGeithus Do you mean "some abstract class that wasn't present in the *answer*?" – Code-Apprentice Jul 28 '12 at 22:56
  • No. He edited the question, so now it includes the "abstract" keyword for class C. Earlier it didn't. – Håvard Geithus Jul 28 '12 at 23:00
  • @HåvardGeithus ahh...I only saw the edited version. Thus the confusion on my end in this conversation. – Code-Apprentice Jul 29 '12 at 18:31
  • Is there a reason you accepted this answer but didn't up vote it? – Code-Apprentice Sep 09 '12 at 20:45
0

You could do

public void DynamicBinding(C cobject){
  c.meth1();
  c.meth2();
}

or little worse variant

public void DynamicBinding(A aobject){
if(aobject instanceof C)
      {
          C myC=(C)aobject;
          myC.meth1();
          myC.meth2();

      }
}

anyway I like what Jorn did better.

m0s
  • 4,250
  • 9
  • 41
  • 64