When do I have to use interfaces instead of abstract classes?

170,148

Solution 1

From Java How to Program about abstract classes:

Because they’re used only as superclasses in inheritance hierarchies, we refer to them as abstract superclasses. These classes cannot be used to instantiate objects, because abstract classes are incomplete. Subclasses must declare the “missing pieces” to become “concrete” classes, from which you can instantiate objects. Otherwise, these subclasses, too, will be abstract.

To answer your question "What is the reason to use interfaces?":

An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design.

As opposed to an interface:

An interface describes a set of methods that can be called on an object, but does not provide concrete implementations for all the methods... Once a class implements an interface, all objects of that class have an is-a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality described by the interface. This is true of all subclasses of that class as well.

So, to answer your question "I was wondering when I should use interfaces", I think you should use interfaces when you want a full implementation and use abstract classes when you want partial pieces for your design (for reusability)

Solution 2

From the Oracle tutorials :

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example.

By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).

Solution 3

Many cases can be implemented in both class types.

Interfaces are usefull when you want to define a class that has to have at least basic functions. Like a real interface for example USB.

interface USB {
    public function sendPower(); //charge iphone for example
    public function sendData(); //itunes
    public function recieveData();
}

Use abstract classes when there are several ways to to implement an object.

abstract class MobilePhone {
    public function isIphone();

    public function charge() {
        //get some power, all phones need that
    }
}

class iPhone extends MobilePhone {
    public function isIphone() { return true; }
}

Solution 4

There are a number of times you might consider using an interface over an abstract implementation

  • When the available abstract implementation doesn't do what you want and you want to create your own
  • when you have an existing class (that extends from other class) and you want to implement the functionality of the interface

Generally speaking, interfaces were introduced to overcome the lack of multiple inheritancy, amongst other things

Solution 5

With support of default methods in interface since launch of Java 8, the gap between interface and abstract classes has been reduced but still they have major differences.

  1. Variables in interface are public static final. But abstract class can have other type of variables like private, protected etc

  2. Methods in interface are public or public static but methods in abstract class can be private and protected too

  3. Use abstract class to establish relation between interrelated objects. Use interface to establish relation between unrelated classes.

Have a look at this article for special properties of interface in java 8. static modifier for default methods in interface causes compile time error in derived error if you want to use @override.

This article explains why default methods have been introduced in java 8 : To enhance the Collections API in Java 8 to support lambda expressions.

Have a look at oracle documentation too to understand the differences in better way.

Have a look at this related SE questions with code example to understand things in better way:

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

Share:
170,148
augmentie123
Author by

augmentie123

Updated on January 03, 2020

Comments

  • augmentie123
    augmentie123 over 4 years

    I was wondering when I should use interfaces.

    Lets think about the following:

    public abstract class Vehicle {
       abstract float getSpeed();
    }
    

    and :

    public interface IVehicle {
      float getSpeed();
    }
    

    I can easily implement both of them, they have the same functionality... BUT I also can add some variables to my vehicle class, which probably should be used in an vehicle (maxSpeed, carType...)

    What is the reason to use interfaces?

    Thanks!

    EDIT: I found a nice link about it in another thread: http://www.thecoldsun.com/en/content/01-2009/abstract-classes-and-interfaces

  • augmentie123
    augmentie123 almost 11 years
    Thanks for your answere! Okay... the second part is a good reason to use interfaces in java, because you cannot extend from multiple classes. But what's about C++ for example, where you can easily extend from as many classes as you want. Why should I use interfaces in this case? Then they are simply "better" than interfaces..
  • Will Jamieson
    Will Jamieson almost 11 years
    inheritance is javas way of dealing with multiple parents
  • augmentie123
    augmentie123 almost 11 years
    But I can always copy the behaviour of the interfaces in my abstract class. I could simply set the method to abstract and the user has to define it himself. Furthermore, if I decide, to change something on my methods and now, I want to have some logic inside of it, which belongs to every class, I can simply edit it, which isn't possible in interfaces
  • Will Jamieson
    Will Jamieson almost 11 years
    Correct ,abstract classes and interfaces though are supposed to be more of a model for classes and not have much logic in them. One of the reasons that you cannot create and object from them
  • rickcnagy
    rickcnagy over 9 years
    These classes cannot be used to instantiate objects, because abstract classes are incomplete. That's really helpful to point out. Thanks!
  • abksrv
    abksrv about 8 years
    What post Java 8 era?
  • Saurabh
    Saurabh almost 8 years
    I think you should use interfaces when you want a full implementation and use abstract classes when you want partial pieces for your design (for reusability) This was helpful
  • nanosoft
    nanosoft almost 8 years
    How is mocking for test difficult with Abstract classes?
  • Shiro
    Shiro almost 7 years
    This is a bit hard to read without any formatting.
  • Buqian Zheng
    Buqian Zheng about 5 years
    interfaces can't have non static fields.