Difference Between Abstract Class And Interface In Java Abstract Class Vs Interface When To Use An Abstract Class And An Interface

Difference Between Abstract Class And Interface In Java


An abstract class in java is a class which has at least one abstract function.

An interface in java  is an entity and not a class which has all its methods as abstract.We cannot have even one implementation of function or method in an interface in java.

An abstract class with all its methods as abstract is close to being an interface.But the concept for usage of both is polls apart in context.

An abstract class looks like :

public abstract class AbstractClassExample1{
abstract void abstractMethod();
// concrete methods are still allowed in abstract classes
void nonAbstractMethod() {
System.out.println("This is a non abstract method.");

Here we can see that we can have both abstract and non abstract methods in abstract class.

An interface looks like :

interface InterfaceExample{

  public void method1();
  public void method2();


Here in the interface all the methods or functions are abstract.

All the methods in interface need to be public .

Now comes the scenario ,when to use an abstract class and when to use an interface.

Scenarios where we want re usability of some of the methods or functions and some methods or functions are left  for the client to implement as per their requirement calls for the use of an abstract class.In such cases client knows that some of the default implementation is already there and some part is up to them how to implement in the form of methods or functions.In such cases it logical to extend the concrete class from an abstract class.

Now comes the scenario where two coders are working for an application or software in which one calls the function or method  in his module which is written by another coder.If in future the first coder who writes the method or function ,for some reason changes the function signature it will hamper the code of the coder who is calling the function.To avoid these type of issues we can use interfaces.If the person who writes the method implements it with interface and the function call is also done with the reference type interlace,then both he coders know the standard to be followed in order to write or call a method.Thus in order to follow some standard we use interfaces.

Number of View :5786

Tags: , ,

  1. No comments yet.

Content Protected Using Blog Protector By: PcDrome.