Core JavaJava Interview Questions

Difference between an abstract class and interface in Java

Posted On
Posted By admin

Another commonly asked interview question is to explain the difference between an abstract class and an interface. So in this blog post, I will give you a detailed explanation.

What is an Abstract Class?

An abstract class is a class that has one or more abstract method. An abstract method is a method that has the “abstract” keyword specified in the method definition. It does not have any method body, it is up to the subclass of the abstract class to provide an implementation for the abstract method.

So why is an abstract class useful?

Sometimes, you will want to create a super class that defines a method that all its sub classes must implement, but it leaves the exact implementation to the sub class. For ex. suppose you have a class called Shape, you can define an abstract method called draw. You can then have sub-classes for various shapes like Rectangle, Triangle, etc which provide implementations for the draw method. So while the “draw” method makes no sense for the Shape class, it makes sense for the Rectangle and Triangle classes and so it makes sense to have the code for the draw me

What is an Interface?

An interface is a java construct that specifies methods without any method bodies. The code for the methods within the interface must be provided by the classes that implement the interface.

Why is an interface useful?

Using  an interface, you can specify what a class must do, but not how it does it.   So you can use it to specify a behavior that classes must implement.

So what are the similarities between abstract class and interface?

  • Both abstract classes and interfaces are used to specify a behavior
  • Both cannot be instantiated

How are they different?

Abstract ClassInterface
An abstract class can have non-abstract methods i.e. methods with method bodiesAll methods within the interface are abstract so it cannot have methods with method bodies.
Only one class can extend an abstract classAn interface can be implemented by several classes
An abstract class can have protected and public abstract methodsAn interface can have only have public abstract methods
Can have instance variablesCannot have instance variables, all variables are implicitly static and final since an interface cannot be instanitated
Can have a constructorCannot have constructors

When to use which?

An abstract class should be used when you want methods in the base class to have some basic default behavior but want to leave it to the subclasses to override these methods. An interface should be used when you want to define a behavior that other classes (probably external) must implement.

 

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Java course here

Also, if you'd like to test your Java knowledge, do check out my practice tests course here


If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment