• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anyone provide real time use case with implementation example.
 
author & internet detective
Posts: 42074
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic