What’s the difference between an interface and an abstract class in Java?

It’s best to start answering this question with a brief definition of abstract classes and interfaces and then explore the differences between the two.

A class must be declared abstract when it has one or more abstract methods. A method is declared abstract when it has a method heading, but no body – which means that an abstract method has no implementation code inside curly braces like normal methods do.

When to use abstract methods in Java?

Why you would want to declare a method as abstract is best illustrated by an example. Take a look at the code below:

/* the Figure class must be declared as abstract 
   because it contains an abstract method  */

public abstract class Figure
{
	/* because this is an abstract method the 
	   body will be blank  */
	public abstract float getArea();	
}

public class Circle extends Figure
{
	private float radius;

	public float getArea()
	{
		return (3.14 * (radius * 2)); 	
	}
}

public class Rectangle extends Figure
{
	private float length, width;

	public float getArea(Figure other)
	{
		return length * width;
	}
}

In the Figure class above, we have an abstract method called getArea(), and because the Figure class contains an abstract method the entire Figure class itself must be declared abstract. The Figure base class has two classes which derive from it – called Circle and Rectangle. Both the Circle and Rectangle classes provide definitions for the getArea method, as you can see in the code above.

But the real question is why did we declare the getArea method to be abstract in the Figure class? Well, what does the getArea method do? It returns the area of a specific shape. But, because the Figure class isn’t a specific shape (like a Circle or a Rectangle), there’s really no definition we can give the getArea method inside the Figure class. That’s why we declare the method and the Figure class to be abstract. Any classes that derive from the Figure class basically has 2 options: 1. The derived class must provide a definition for the getArea method OR 2. The derived class must be declared abstract itself.

A non abstract class is called a concrete class

You should also know that any non abstract class is called a concrete class. Knowing your terminology defintely pays off in an interview.

Now that we’ve explored the abstract method/class concepts, let’s get into the concept of interfaces and how they differ from abstract classes.

Java interface versus abstract class

An interface differs from an abstract class because an interface is not a class. An interface is essentially a type that can be satisfied by any class that implements the interface.

Any class that implements an interface must satisfy 2 conditions:

  • It must have the phrase "implements Interface_Name" at the beginning of the class definiton.
  • It must implement all of the method headings listed in the interface definition.

This is what an interface called "Dog" would look like:

public interface Dog
{
	public boolean Barks();

	public boolean isGoldenRetriever();
}

Now, if a class were to implement this interface, this is what it would look like:

public class SomeClass implements Dog
{
	public boolean Barks{
	// method definition here
	
	}

	public boolean isGoldenRetriever{
	// method definition here
	}
}

Now that we know the basics of interfaces and abstract classes, let’s get to the heart of the question and explore the differences between the two. Here are the three major differences:

Abstract classes and inheritance


1. Abstract classes are meant to be inherited from, and when one class inherits from another it means that there is a strong relationship between the 2 classes. For instance, if we have an abstract base class called "Canine", any deriving class should be an animal that belongs to the Canine family (like a Dog or a Wolf). The reason we use the word "should" is because it is up to the Java developer to ensure that relationship is maintained.

With an interface on the other hand, the relationship between the interface itself and the class implementing the interface is not necessarily strong. For example, if we have a class called "House", that class could also implement an interface called "AirConditioning". Having air conditioning not really an essential part of a House (although some may argue that point), and the relationship is not as strong as, say, the relationship between a “TownHouse” class and the "House" class or the relationship between an “Apartment” class that derives from a “House” class.

Because a TownHouse is a type of House, that relationship is very strong, and would be more appropriately defined through inheritance instead of interfaces.

So, we can summarize this first point by saying that an abstract class would be more appropriate when there is a strong relationship between the abstract class and the classes that will derive from it. Again, this is because an abstract class is very closely linked to inheritance, which implies a strong relationship. But, with interfaces there need not be a strong relationship between the interface and the classes that implement the interface.

Interfaces are a good substitute for multiple inheritance


2. Java does not allow multiple inheritance – see the discussion on Java Multiple Inheritance if you need a refresher on this. In Java, a class can only derive from one class, whether it’s abstract or not. However, a class can implement multiple interfaces – which could be considered as an alternative to for multiple inheritance. So, one major difference is that a Java class can inherit from only one abstract class, but can implement multiple interfaces.

Abstract classes can have some implementation code

3. An abstract class may provide some methods with definitions – so an abstract class can have non-abstract methods with actual implementation details. An abstract class can also have constructors and instance variables as well. An interface, however, can not provide any method definitions – it can only provide method headings. Any class that implements the interface is responsible for providing the method definition/implementation.

When to use abstract class and interface in Java

Here are some guidelines on when to use an abstract class and when to use interfaces in Java:

  • An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation to derived classes.
  • 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.
  • If you think you will need to add methods in the future, then an abstract class is a better choice. 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 API will not change for a while.
  • Interfaces are also good when you want to have something similar to multiple inheritance, since you can implement multiple interfaces.


Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

FOLLOW Varoon Sahgal, Author of ProgrammerInterviewon

81 Responses to “Interface vs. Abstract Class”

  1. Smill says:

    With the update of Java 8, you can actually give a method defintion in an Interface as a workaround to updatin API. Also, there is a key diffrence as to how classes and interfaces are compiled. That being said, this was very helpful. Thank you.

  2. suzi says:

    excellent! thank you!!! :)

  3. Upendra Handa says:

    Thanks for the good explanation. I got my answer here.

  4. OmAr NaTour says:

    Sir , it worked with me when I implemented a body for an interface static method , what does it mean ?

  5. rajpal says:

    simple and clear explanation! well done!

  6. Ravi Thumma says:

    Excellent information. The differentiation is awesome

  7. Sarath Raavi says:

    Concrete methods perspective..!!
    An abstract class can have concrete methods along with abstract methods. so even if we add any concrete methods to the abstract class, implemented classes need not to be changed as we were only responsible to provide the implementations for the abstract methods not for concrete methods.

  8. Malik Hassan says:

    It is The Best

  9. Mohammad Zain says:

    Great…Nicely explained!!

  10. Upendra Handa says:

    Can you explain a bit more about this one you said

    “If you think you will need to add methods in the future, then an abstract class is a better choice. 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.”

    As far as I know, the classes which inherit from an abstract class need to define all its methods otherwise they won’t compile. So howcome is it a better choice?

  11. Usha says:

    Very good article. It explains the concepts clearly with simple examples.

  12. benitta ruphus says:

    really good post… got all the answers I was looking for

  13. Rajendran says:

    Fantastic explanation – and most important the reference links to other related stuff! Outstanding!

  14. Oliver Lockwood says:

    It’s worth noting that with the advent of Java 8, interfaces can provide default implementations. This page provides a reasonably good explanation: http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/

  15. gulrea says:

    I loved the explanation!! It is very concise and clear. I loved the idea of house, townhouse, and air conditioner. We can even think of an airplane. It also implements air conditioner but has nothing to do with house.

  16. Aaditya Hardenia says:

    nice blog… helpful

  17. Ahmad says:

    If you think you will need to add methods in the future, then an abstract class is a better choice.

    i think this is better
    if you add non-abstract methods then an abstract class is a better choice.

  18. NoOne says:

    very short and good-explained. tnx

  19. Akhilesh Mishra says:

    good explanation……

  20. Sachin says:

    Good article.. Easy to understand…..

  21. J Sreedhar says:

    Thanks for your explanation

  22. Pravesh Jain says:

    Great article. The only turn off about this is the incorrect calculation of area of a circle.

    Please update from 3.14 * (radius * 2) to 3.14 * (radius ^ 2).

  23. Pravesh Jain says:

    You’re right in saying that an Overriding method must have the same definition as the super class. Therefore the method getArea() here is not Overriding the method from the Figure class. It is merely Overloading it!

  24. sunil says:

    Just Awesome. Complete description for interfaces and abstruct classes. Got all my concepts clear

  25. SARAVANAN says:

    Meets most of the interviewer expectations

  26. saravanan says:

    Best Explanation and Example

  27. Rakesh says:

    super

  28. fayaz says:

    check the below: its written in very simple way relating with real world

    http://samplecoder.blogspot.in/2014/11/difference-between-abstract-class-and.html[^]

  29. Avinash says:

    I’m new to OOP in Java and I understood more about interfaces and abstract after reading this. Thanks!

  30. kishore rajendra says:

    amazing article. Best possible comparision with all small details. I am finding this site very useful.
    Thanks for the post.

  31. pratyasa says:

    Thanks a lot…..finally got the perfect answer…

  32. rajesh says:

    this is very simple and easy way to understand abstract and interface

  33. Santhoshni says:

    Couldn’t answer this one in the interview.. now u have clarified.. thank u so much.. To the point!!!

  34. Divi Pris says:

    this is really good

  35. Guest says:

    Thoroughly touched the Topic. Thanks

  36. Common Man says:

    Hi admin,
    I have a small doubt, you have added the definition as
    getArea(Figure other), it is not correct as the over riden method should have same definition as super class method.
    Please correct me if am wrong else the post.
    Thanks.

  37. wonder says:

    Very Good explanation!!!

  38. manish says:

    pretty awesome.

  39. virusk330 says:

    good answer like it

  40. kalees says:

    IN which one we cant to inherit?

  41. HK says:

    In future if a method is added in abstract class and interface, both of their derived classes are forced to implement the new methods!

  42. cuniquek87 . says:

    thanks for the help!

  43. shivam says:

    yup dat is!

  44. Rathinavel Pandiyan says:

    it really really helped …! thank you !!

  45. Gajendra Vaishnaw says:

    very very easy Method to Describe..Wonderful..

  46. Sujaj says:

    Very nice explanation !

  47. Prem says:

    Very easy to understand :) Thank you very much

  48. Dinesh Sood says:

    Superb explanation. Thanks a lot.

  49. Phirrus says:

    awesome explaination

  50. Saurabh says:

    The direct differnce can be found at below location :

    http://javatopics77.blogspot.com/p/difference-between-interface-and.html

  51. Gabriel Diac says:

    Thank you!

  52. Sham Soudzoi says:

    please can i a question?

  53. Aman says:

    abstract class can have instance variables. It cannot be instantiated means we cannot create an object of this class.

  54. hlobaz says:

    correction, paragrath number 3 u stated that an abstract class can have and instance variable. thts not the case cuz abstact class cannot be instantiated

  55. Ben Stuart says:

    Mmm very nice refresher. Its has bean some time since I have read about interfaces and abstract classes.

  56. Vasu says:

    Simply Awsome….Trying to know the difference from last 1 year..You stopped me..

  57. swati says:

    very nicely explained…. got da answer for what i was looking for

  58. amit says:

    ya thats true it will show a compile time error

  59. Akshay says:

    Super cool!!!.. It solved almost all doubts. Thanks a lot!!!

  60. Satish Reddy says:

    Nice explaination.

    You can find similar java questions and answers for interfaces and abstract classes here..

    http://skillgun.com/java/interface-abstractclass/interview-questions-and-answers

  61. Snarfs says:

    Great explanation!

  62. kumaresan says:

    I read many tutorials. i did not like them, but i am happy now. it's brieeeeef explanation and thank you.

  63. Kwesi says:

    Splendid explanation. I finally found a saviour. Thanks

  64. pritam says:

    In the rectangle class how can you pass the as Figure in the getArea()?the superclass method does not have parameter in its method…

  65. mibrahim says:

    I read many articles about the same subject and this is the best one. Thank you :)

  66. T Dog says:

    Outstanding. I looked all over the place for a simple explanation and this is by far the best!!!

  67. nithin says:

    i m ur fan.

  68. varoon10 says:

    Glad it helped – thanks!

  69. varoon10 says:

    Thanks Star!

  70. varoon10 says:

    Thanks Samin!

  71. varoon10 says:

    Thanks Omkar!

  72. varoon10 says:

    Thanks!

  73. Star says:

    Thanks for your explaination

  74. Samin says:

    This was the best explanation about differences between abstract and Interface

  75. maruthi says:

    Super explaination

  76. Omkar Joshi says:

    great explained.!!

  77. nikhilnikky says:

    Nice Explanation….
    Thanks

  78. Destined2workhard says:

    well explained!!

Leave a Reply

*