X Tutup
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Ron McLeod
Saloon Keepers:
  • Tim Holloway
Bartenders:

Difference between Abstract Class and Interface in Java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Message Content:
Hi everyone,
I am currently learning Java and I am confused about the difference between an abstract class and an interface.
From what I understand:
An abstract class can have both abstract and non-abstract methods.
An interface contains only abstract methods (but I read that from Java 8 it can also have default and static methods).
Here is a small example:

My doubts are:
When should I use an abstract class instead of an interface?
Can a class extend multiple abstract classes?
What are the real-time scenarios where interfaces are preferred?
I would appreciate if someone could explain with simple examples.
Thank you!
 
Marshal
Posts: 82157
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naveen Kulanthaivel wrote:. . . When should I use an abstract class instead of an interface?

When you have default functionality you want a limited number of types to implement, then an abstract class is probably better. Example:-All Vehicles have a speed, so you would waqt that as a non‑abstract method. You don't want to instantiate Vehicle directly, so you mark the class abstract.

Can a class extend multiple abstract classes?

No.

What are the real-time scenarios where interfaces are preferred? . .

When you don't know how many implementing types you are likely to have. You might make Engine an interface. I wrote about an Engine interface here.
 
Saloon Keeper
Posts: 29101
215
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having more than one direct base class (abstract or not) is known as multiple  inheritance. The C++ programming language supports multiple inheritance, and it can get very, very messy. Most notably when two or more base classes implement the same method but with conflicting actions.

The designers of Java saw that and that is why Java has Interfaces. They offer the same benefits as multiple inheritance without the mess.
 
Master Rancher
Posts: 5291
87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interestingly, thanks to default methods, we do now have something like multiple inheritance of implementations in Java.  They have a fairly simple fix for the problem Tim references: if there are two or more default implementations for the same method, then a compiler error forces you to explicitly specify how the method should be implemented.  For example:

This gives an error message:

This can be fixed by providing an implementation.  If you want to use one of the conflicting default implementations, there's a simple syntax:

This now compiles and works fine.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic
X Tutup