When to use abstract class or interface?

29,827

Solution 1

Interface is used when you only want to declare which methods and members a class MUST have. Anyone implementing the interface will have to declare and implement the methods listed by the interface.

If you also want to have a default implementation, use abstract class. Any class extending the abstract class will have to implement only its abstract methods and members, and will have some default implementation of the other methods of the abstract class, which you may override or not.

--EDIT - forgot to mention, Earwicker reminded me

Finally, you can implement as many interfaces as you want, but only extend one class (being it abstract or not). Keep that in mind before choosing.

Solution 2

The key difference is that you can implement multiple interfaces in a class, but only extend a single abstract class. This is because an abstract class can also define fields that store data, whereas an interface cannot.

Solution 3

An abstract class is a class, that has atleast one abstract method or you can also make all your methods as abstract. Obviously it cannot be instantiated. You have to inherit from an abstract class and implement the abstract methods in the inheriting class (i.e, the class extending the abstract class).

Interfaces are not classes at all (so don't call them interface class). Interfaces define the signature of methods without any implementation. Also interfaces have no member-fields. If you implement an interface in a class, you have to provide implementations for all the methods provided by the interface.

It makes sense to define a generalized API for some stuff, that can have completely different implementations. Abstract classes are more useful for classes that do mainly the same, but have some subtle differences. You can combine both approaches.

A good example is the collections framework of the Java class-library. You have the interface List, that defines how Lists have to behave. Some implementations are for instance ArrayList and LinkedList. As they behave similar, the stuff that works the same for both is implemented in the abstract class AbstactList, both inherit this.

Solution 4

See Interface is basically a "Contract". When you are defining an interface you are defining a Contract. Where abstract classes are extended, interfaces are Implemented.

Let's consider an example.

public interface Friend {
void hello();
}

Now you have defined a contract which says that any class which wants to implement Friend needs to provide a definition for method hello().

Here is an implementation:

public class myFriend implements Friend {
public void hello()
println("Done");
}

Now myFriend has fulfilled the contract. Now the question is: Where should interfaces be used?

Interfaces help you define a behavior which must be implemented. Say you have a class A which defines some functionality. You want the other classes to use this class functionality only if they define particular behavior (methods). You enforce this restriction in term of interface.

Solution 5

SamuelCarrijo seems to have answered this question well.

In addition for Java, some frameworks require an interface to work with. I'm thinking of (say) dynamic proxies, or some client/server proxying frameworks. This is because they use introspection on the object to determine methods implemented by the interfaces implemented by the object. So occasionally you have to implement an interface for an object where, perhaps, you wouldn't normally bother.

Note this reason for interfaces is specific to Java.

Share:
29,827
JavaResp
Author by

JavaResp

java code developer

Updated on April 24, 2020

Comments

  • JavaResp
    JavaResp about 4 years

    Why are abstract or interface classes created, or when should we use abstract or interface classes?