Difference between abstract class and interface

DifferencesJava 8Server Side Programming

An abstract class can contain both abstract and non-abstract methods, whereas an Interface can have only abstract methods. Abstract classes are extended, while Interfaces are implemented. Read through this article to find out the other differences between an Abstract Class and an Interface and how they are used in real programs.

What is an Abstract Class?

An abstract class acts as a template that stores the methods and data members of a program. You should use an abstract class when you expect that it will inherited by different sub-classes with common methods and fields.

Abstract classes may or may not contain abstract methods, i.e., methods without body. But, if a class has at least one abstract method, then the class must be declared abstract.

Abstract classes cannot be instantiated, but they can be inherited. To use an abstract class, you have to inherit it from another class, provide implementations of the abstract methods in it. You have to use the keyword abstract to create an abstract class or an abstract method.

Abstract Method

An abstract method will not have any code in the base class; the code will be added in its derived classes. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it. If you do not, then the sub-class too have to be declared abstract.

Extending an Abstract Class

Here is a simple Java program of an abstract class containing an abstract method and a non-abstract method −

Example

// abstract class
abstract class Calculator{

   // abstract method
   abstract int add (int a, int b);

   // non-abstract; method with implementation
   int mul (int a, int b){
      return a*b;
   }
}
// Inheriting the abstract class
class Sum extends Calculator{
   public int add (int a, int b){
      return a+b;
   }
}
public class test{
   public static void main (String []args){
      Calculator obj = new Sum();
      System.out.println("The sum is: " + obj.add(2,3));
      System.out.println("The multiplication is: " +
      obj.mul(2,3));
   }
}

Output

It will produce the following output −

The sum is: 5
The multiplication is: 6

What is an Interface?

An interface is a reference type in Java. It is similar to a class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.

Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

An interface is similar to a class in the following ways −

  • An interface can contain any number of methods.

  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.

  • The bytecode of an interface appears in a .class file.

  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

An interface is different from a class in several ways, including −

  • You cannot instantiate an interface.

  • An interface does not contain any constructors.

  • All of the methods in an interface are abstract.

  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.

  • An interface is not extended by a class; it is implemented by a class.

  • An interface can extend multiple interfaces.

Interfaces have the following properties −

  • An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.

  • Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.

  • Methods in an interface are implicitly public.

Implementing an Interface

The interface keyword is used to declare an interface. Here is a simple example of how to declare and implement an interface in Java −

Example

// Declaring the Interface
interface Animal {
   public void eat();
   public void travel();
}
// Implementing the Interface
public class MammalInt implements Animal {
   public void eat() {
      System.out.println("Mammal eats");
   }
   public void travel() {
      System.out.println("Mammal travels");
   }
   public int noOfLegs() {
      return 0;
   }
   public static void main(String args[]) {
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
}

Output

It will produce the following output −

Mammal eats
Mammal travels

Conclusion

Abstract classes and Interfaces are important concepts in object-oriented programming. Here we used Java programming examples to highlight how abstract classes are different from interfaces and how developers use these concepts.

raja
Updated on 28-Jul-2022 08:34:09

Advertisements