Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Inheritance in Java

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code compiles fine. I always thought it is not possible to state more than one class after "extends"?
interface Int_A {}
interface Int_B {}
class A{}
class B{}
class C extends A, B {}
interface Test extends Int_A , Int_B {}
Appreciate your answers:
Thomas
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can't extend more than one class but you can implement more than one interface. I think you know that already. The code you posted flags a compile time error when it reaches
class C extends A, B {}
Maybe you should try to compile it again.
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention that an interface can extend more that one interface.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces can always extend other interfaces, since this presents no multiple inheritance problem. Whoever first implements an interface must provide implementations for everything in that interface, PLUS, everything in all the superinterfaces (i.e. the interfaces your interface extends, and any that *they* extend, and so on all the way up).
Just a side note about one place this is used...
This is used especially in EJBs where you might have a business interface (with just business logic) and then a component interface that extends both Remote (which it must, for RMI) and also the business interface. Then your enterprise bean can extend just the business interface and everyone is happy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic