Image Image Image Image Image Image Image Image Image Image

Mudassir Shahzad | July 21, 2017

Scroll to top

Top

3 Comments

Abstract Classes Vs Interfaces – When to use which

Abstract Classes Vs Interfaces – When to use which
Mudassir Shahzad

This is one of the mostly asked question in job interviews or other professional gatherings.

Most of the candidates are able to tell the difference but they get stuck in the second part, WHEN TO USE WHICH ?

Let’s first start by defining both.

abstractvsinterfaces

 

Interface

 

An interface is a contract:

the guy writing the interface say “hey, I accept things looking that way”,

and the guy using the interface says “OK, the class I write looks that way”.

  • An interface is an empty shell
  • There are only the signatures (name / params / return type) of the methods
  • The methods do not contain anything
  • The interface can’t do anything
  • It’s just a pattern

Consider this pseudo code

Abstract Class

 

Abstract classes, unlike interfaces, are classes. There are more expensive to use because there is a lookup to do when you inherit from them. 

Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It’s more about a guy saying “these classes should look like that, and they got that in common, so fill in the blanks!”.

For example

So the key difference between both are:

 

  1. Abstract classes can have consts, members, method signatures (declaration only) and defined methods, whereas interfaces can only have consts and methods declarations(no definition).
  2. Methods and members of an abstract class can be defined with any visibility(public, private or protected), whereas all methods of an interface must be defined as public.
  3. When inheriting an abstract class, the child class must define the abstract methods, whereas an interface can extend another interface and methods don’t have to be defined.
  4. A child class can only extend a single abstract (or any other) class, whereas an interface can extend or a class can implement multiple other interfaces.
  5. A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility.

 

When to use which

 

  • An abstract class is good when you want all the child classes to have a basic functionality.
  • An abstract class is also good if you want to be able to declare non-public members. In an interface, all methods must be  public.
  • At the first glance of any requirement we are partially clear on what exactly is to be build but we know what to build on an abstract level.  So, you will definitely need more methods in the future. In this case, abstract classes are good because if you add new method headings to an interface, then all of the classes that already implement that interface will have to be changed to implement the new methods. That can be quite a hassle.
  • Interfaces are a good choice when you think that the code will not change for a while
  • Java does not support multiple inheritance but it can be achieved using interfaces. So, interfaces are good when you want to achieve multiple inheritance (multiple parent classes).

 

 

References

 

http://www.programmerinterview.com/

http://www.agile-code.com/blog/wp-content/uploads/2012/05/abstract_class_interfaces.png

 

 

If you liked this article, please share it in your network.

Related posts:

Comments

  1. Good work. I’ve never seen such easy to understand tutorials. You have described everything in a very friendly way
    Thanks bro

    • Mudassir Shahzad

      You are welcome.

    • Mudassir Shahzad

      Thanks for the comment

Submit a Comment

Too many requests