Special welcome gift. Get 50% off your first courses with code “AS50”. Find out more!

HomeInterviewWhat is the difference between an Interface and an Abstract class?
product 6

What is the difference between an Interface and an Abstract class?

Java provides and supports the creation of both the abstract classes and interfaces. Both implementations share some common characteristics, but they differ in the following features:

  • All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.
  • A class may implement a number of Interfaces but can extend only one abstract class.
  • In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
  • Abstract classes can implement interfaces without even providing the implementation of interface methods.
  • Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
  • Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
  • An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated but can be invoked if it contains the main method.

Also, check out the Abstract class and Interface differences for JDK 8.

Interface Abstract Class
An interface has the method signatures. It does not have any implementation.Abstract class has the abstract methods and details to be overridden.
A Class can implement multiple interfacesIn this case, a class can extend just one abstract class
Interface has all abstract methods.Non abstract methods can be there in an abstract class.
Instance properties cannot be there in an interface.Instance properties can be there in an abstract class.
An Interface is publicly visible or not visible.An abstract class can be public, private and protected visibile.
Any change in the interface will impact the classes implementing the interface.Adding a method to an abstract class and implementing it does not require change in the code for derived classes.
An Interface cannot have constructorsAn abstract class can have constructors
Interfaces are slow in terms of performanceAbstract classes are fast in execution of the methods in the derived classes.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

Categories

ads sidebar 1

You May Also Like

Oracle has several modes for shutting down the database: In normal mode, the database is shut down by default. It...
Materialized views are items that contain condensed sets of data from base tables that have been summarized, clustered, or aggregated. They...
Every database in Oracle has a tablespace called SYSTEM, which is generated automatically when the database is created. It also...