Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Inheritance in interface ?

 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not allow multiple inheritance for classes, but for interface it's possible to inherit from multiple interfaces ?. What the reason for this ?. Why does java allow multiple inheritance in interface and not for classes ?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not allow multiple inheritance for classes, but for interface it's possible to inherit from multiple interfaces ?. What the reason for this ?. Why does java allow multiple inheritance in interface and not for classes ?
The main motivation for interfaces in java's beginning was to get away from multiple inheritance. If you really think about one interface extending another, it is not so much inheritance as it is concatenation. Class inheritance is a lot more than merely summing all the methods and instance variables. Classes must be instantiated thru the whole hierarchy from most specific instance to most general (always an instance of Object for java). Interfaces cannot be instantiated. Further consider the almost intolerable restriction that limiting interface implementation to a single interface. Class design would be greatly diminished. Having a class implement more than one interface is semantically equivalent to one interface extending the remaining interfaces.
The reason java does not allow multiple class inheritance is because it creates more problems than it solves for compiler, runtime and programmer. Take a look at this thread for a discussion on that subject.
 
Ahetuki Neti
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moreover, while writing anonymous inner classes you will realize the importance of multiple inheritance of interfaces!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic