Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Difference between abstract class and Java 8 Interfaces ?

 
Ranch Foreman
Posts: 1791
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before Java 8 the difference between abstract classes and interfaces was that abstract classes can have concrete methods too in addition to abstract methods while interfaces were completely abstract.But in Java 8, default methods were introduced and that difference seem to have gone as now interfaces too aren't completely abstract.
So what is the difference between Java 8 Interfaces and abstract classes ?

Thanks
 
Saloon Keeper
Posts: 6630
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What has not changed is that you can extend only one class, whereas you can implement multiple interfaces.
 
Master Rancher
Posts: 3692
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other miscellaneous differences: abstract classes can have constructors; interfaces can't.  Abstract classes can have protected and package access; interfaces can't.  When you override a method in an abstract class, you can still access the original implementation using super; if you implement an interface with a default method and you provide a new non-default implementation of that method, you can't access the default implementation. There are probably some others that are even less noticeable.  
 
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more about protected and package‑private access.
An interface with one abstract method can have default/static methods added and remain a @FunctionalInterface. This is a good example.
 
Bartender
Posts: 2697
130
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:But in Java 8, default methods were introduced and that difference seem to have gone as now interfaces too aren't completely abstract.


Technical stuff aside, I think there's a difference in intent as well. While an Interface is a contract, it lacked the functionality of adding new methods to it without breaking existing code. This is why default methods were added. I would treat them as an exceptional case e.g. where an API is live and is used by other developers.
 
Mike Simmons
Master Rancher
Posts: 3692
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please explain more about protected and package‑private access.



I'm not sure what there is to add.  In an interface, you can't have a protected method, or a package private method.  If you omit the access modifier in an interface, it's implicitly public.  In an abstract class, you can have a protected method, or if you can omit the access modifier in a class, and then you get package private access.

Good point that interfaces can be functional interfaces, and abstract classes can't.  That allows a lambda expression to be used in place of an interface type... a nice trick which you can't do with an abstract class.
 
Mike Simmons
Master Rancher
Posts: 3692
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:

Monica Shiralkar wrote:But in Java 8, default methods were introduced and that difference seem to have gone as now interfaces too aren't completely abstract.


Technical stuff aside, I think there's a difference in intent as well. While an Interface is a contract, it lacked the functionality of adding new methods to it without breaking existing code. This is why default methods were added. I would treat them as an exceptional case e.g. where an API is live and is used by other developers.



Good point that that's basically why default methods were added.  However I don't think it's all that exceptional... it should be regarded as a normal part of a successful API's lifetime.  It also makes it easier for people to make new implementations of an API.
 
Campbell Ritchie
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . . In an interface, you can't have a protected method, or a package private method. . . . .

Aaaaaaaaaaaaaaaaaaaah! I thought at first maybe you were referring to access modifiers for the interface as a whole.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic