• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between Abstract Class and interface in Java 8

 
R K Singh
Ranch Hand
Posts: 5388
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,

I tried to find answer outside wide world, but I failed, so I am asking this question here:

What is the difference between Abstract Class and interface in java 8, if it allows interface to have implementation of method also ?

Thanks for helping in understanding the new concept.
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 96
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the (programmer) user point of view, the main difference is that everything in an interface is public, whether an abstract class may have private, package private, or protected members.
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 9, it's likely that interfaces will be allowed to have private methods.

The two most important differences are that an abstract class can have instance fields but that a class can extend only one abstract class, and unlimited interfaces.

You should prefer the use of interfaces as much as possible. Use abstract classes only for default implementations that require private state.
 
R K Singh
Ranch Hand
Posts: 5388
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All enhancements to java, I am sure will turn out to be useful.

But being a old student, I still feel that by allowing public/private methods to interface we are somehow taking interface to a point where the only difference with class will be that interface will not have state (member variables). And who knows Java 11 may have that too ;-)

Anyways, java was proud to have solution for diamond shape, but now we have resolution operator in java also.

Thanks a lot for your inputs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!