• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple inheritence in java

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Some books says (indian authors ),through interfaces we will achieve a form multiple inheritance. IS this true??
Some people strongly argue there is no multiple inheritance in java..??
they will say what what you are getting by extending an interface, it is just an agreement !!!

can any one explain

thanks
rajendar
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces do not allow you to inherit functionality, they merely define a type. By implementing an Interface a class declares to other classes that it can be assigned to variables of the interfaces type. A class does not inherit any functionality from an interface though. When your class implements an interface it must define the implementation for all of the methods declared in the interface or it must be declared abstract (and thus cannot be instantiated). A class can only inherit functionality (i.e an implemented method that can be called) by extending a super class, and each class can only do this once.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote:Interfaces do not allow you to inherit functionality, they merely define a type. By implementing an Interface a class declares to other classes that it can be assigned to variables of the interfaces type. A class does not inherit any functionality from an interface though. When your class implements an interface it must define the implementation for all of the methods declared in the interface or it must be declared abstract (and thus cannot be instantiated). A class can only inherit functionality (i.e an implemented method that can be called) by extending a super class, and each class can only do this once.


Well, that was true until Java 8. That introduced default methods in interfaces, where you can actually have implementations in the interface and thereby inherit that functionality. It also introduces the deadly diamond of death in Java, but I think they've solved that not too badly.

In short, when a class implements interfaces A and B and both have default implementations for method X, then the class must re-implement method X. It can then choose to call the default implementation of one of the interfaces explicitly by calling A.super.X and/or B.super.X.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed you're right, I'd completely forgotten about Java 8.

In fact thinking about it, I did try that out in Eclipse a few weeks ago to see if the diamond of death was possible, and Eclipse flags it as a compilation error without suggesting that it can be fixed by overriding the default method in the class implementing the interfaces. I had assumed it was just generally disallowed. Thanks for pointing this out.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for mentioning about Java 8. I am always interested to know why Java has introduced default method definition in Interface.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It allows you to add new methods to an interface without breaking backwards compatibility. Without default methods any existing code would fail to compile if you add new methods to an interface.

This allowed them to update the Collections interfaces with the new Streams API.
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote:It allows you to add new methods to an interface without breaking backwards compatibility. Without default methods any existing code would fail to compile if you add new methods to an interface.

This allowed them to update the Collections interfaces with the new Streams API.




hi Mike what is meant by backward compatibility here.??

Abstract methods are also giving same kind of functionality, then what is difference between these two ???

thnks rajendar
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe what Mike is saying is that in the newest (or at least a newer) release of Java, they wanted to add some methods to the Collections interface. But if they simply added them, then the first time you compiled your old code with the new java, it would break, since the code you wrote 3 or 5 years ago won't implement the new methods.

But, since default implementations are allowed, they can add the new methods, AND a default implementation. So now when you compile your legacy code, even though YOU didn't implement these new methods, they ARE implemented, so you can still compile.
 
reply
    Bookmark Topic Watch Topic
  • New Topic