Difference Between Interface and Abstract Class in Java

This is one of the most frequently asked question in the interview. Many interviewers will not leave this topic. So lets see the difference between them

  • The main difference is that the Interfaces are implicitly abstract and cannot have implementation, whereas the abstract class can have the concrete class.
  • Interface are implemented using the “implements” keyword. Abstract class can be extended using the “extends” keyword.
  • All the variables in  the Interface are by default Static and Final whereas the Abstract can have other access specifiers as well.
  • The methods in the Interface is by default to Public but Abstract class can have non-public(private,protected..) methods too.
  • An Interface can extend other Interface only, an Abstract class can extend other java classes and implement interfaces.
  • Interfaces are absolutely abstract and cannot be instantiated but the abstract class cannot be instantiated but it can be run if it has a main() method.The below code will get executed successfully and will get you the output as “Inside Main method” and followed by “Abstract method called”.  So Abstract class can have a main method but still you cannot instantiate the class.
abstract class AbstractEg {

	public static void main(String aa[])
	{
		System.out.println("Inside main method");
		disp();
	}
	abstract void disp1();

	public static void disp()
	{

		System.out.println("Abstract method called");

	}

}
  • A Java class can implement multiple interface but can extend only one abstract class.

Before ending up this. there comes one more question regarding the usage

When to use Interface and Abstract class?

You can use interfaces when you want a full implementation and use abstract classes when you want partial implementation for your design.

Leave a Reply

Your email address will not be published. Required fields are marked *