• Post Reply Bookmark Topic Watch Topic
  • New Topic

How should I explain the difference between an Interface and an Abstract class?  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

How should I have explained the difference between an Interface and an Abstract class with Real Time Use Case and Example ? Please explain with real time use case and examples.

I know the difference b/w Abstract Class and Interface as below:

Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.

Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.

Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc.

A Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.

An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

A Java class can implement multiple interfaces but it can extend only one abstract class.

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You realise the definition of an interface changed in Java8? We now have default methods and static methods (you didn't mention static methods, but classic interfaces can't have static methods), and it is likely that there will be changes in future to permit private methods and variable fields.
An interface may extend several interfaces with special provisions if they have multiple methods or fields with the same names.
This question comes up frequently, so we have an FAQ about it. Look at this page of FAQs, and it is about halfway down.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:[...] it is likely that there will be changes in future to permit private methods and variable fields.

Fields in interfaces are always implicitly static and final. Changing this in the future would break existing code. I doubt they will ever allow variable/instance fields in interfaces, simply because it's a bad idea, even if it wasn't hard to implement.
 
Gp Yadav
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much
 
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Changing this in the future would break existing code.

I think he meant private methods and private variable fields. If the fields are private, only the default methods would be able to access them and I don't think that would break code.
 
Gp Yadav
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone provide real time use case with implementation example.
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Oracle trails tutorial gives examples:
Abstract Class
Interface

As you can see, an abstract class is more of a thing. And interface is more of behavior that a thing should have.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:. . . I think he meant private methods and private variable fields. . . .
Yes, I think I did. It was a long time since I saw that proposal about private methods, so I had forgotten most of the details.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!