pure abstract class and interface

Viewed 14273

Can anyone tell me what exactly the difference between an completely abstract class and an interface?

An Abstract class can also have all its methods as abstract. An interface has all its methods as abstract. What is the main difference between the two in this scenario?

If there is difference between a pure abstract Class and interface? What is the use of interface? Where interface is being used we can make use of pure abstract class?

11 Answers

To complete the former answers :

An interface is a "contract". If a class implements an interface it have to propose all the services listed in the interface.

An abstract class is a skeleton. It defines a certain way its extended classes will work while letting them some free space (the abstract methods) to be unique.

A pure abstract class doing the same thing as a interface but have the problem of unique extending so, for me, it have no interest

Every interface is implicitly abstract: Every method declaration in the body of interface is implicitly abstract and public.

An abstract class has methods that can contain implementation. Abstract methods can be either public, protected or default access (package visible). Unlike interfaces abstract classes can contain fields that are not static and final.

Also see:
Interfaces vs Abstract classes and the Java tutorial

In Java and C#, one can use multiple interfaces to derive from and only a single class to inherit from,

An abstract class can provide an implementation, i.e. (public, protected, private) method bodies. An interface can just declare public method signatures. These methods have to be realized (in the form of method bodies) by classes implementing the interface.

There are three differences:

  1. Interfaces can only declare public methods (i.e. no protected or package-private visible methods) and can not declare any fields
  2. Subclasses can only extend at most one abstract class, but can implement any number of interfaces
  3. The abstract class can also have implementations for some or all of the methods

I'm just going to address one point (mainly because the other questions have been addressed already):

"Where interface is being used we can make use of pure abstract class?"

In theory, you could. However, you will lose flexibility and loose coupling to some extent. It's far more preferable to code to interfaces and pass those around, especially in Inversion of Control (IoC) scenarios and from an integration point of view, as this allows far greater extensibility.

You can use Interface for multiple inheritance, but you can't use abstract class for multiple inheritance.

All the methods in Interface is public by default, by in abstract class, only the methods which you've set as an abstract need to be declared public.

A class can implement multiple interfaces, but only extend from one class (abstract or otherwise). If you need to specify an interface, then you should use an interface, so that classes may implement multiple of your interfaces.

Related