Difference Between Similar Terms and Objects

Difference between Abstract Class and Interface in Java

Abstract class and interface both are primarily used for abstraction, however they are very different from each other. Both are core part of Java but have their own application uses. While both terms may be synonymous with each other, they cannot be used interchangeably.

There are significant differences between the two. While an interface method is abstract by default, an abstract can contain both abstract and non-abstract methods. Well, since Java 8, interfaces also have method implementations and they can have both static and default methods in Java. Let’s study the difference between the two in detail.

Difference between Abstract Class and Interface in Java

What is an Abstract Class in Java?

An abstract class in Java is a class that is declared abstract – it may or may not contain abstract methods. It is quite similar to a Java interface except that fact that it can contain default method implementation. Like in object-oriented programming, abstraction in Java is achieved using abstract classes and interfaces. In Java, abstraction means hiding the irrelevant details from the user to focus only on the essential details to increase efficiency thereby reducing complexity. The keyword “abstract” is used to create an abstract class just before the class keyword in the class declaration.

An abstract class may or may not contain abstraction methods (methods without body), but if a class has at least one abstraction method, it must be declared abstract. However, non-abstract methods can only also be used in an abstract class, but only to implement default behavior. The methods in an abstract class can have access modifiers such as private, public, static and protected, depending on the visibility level. An abstract class cannot be instantiated and it does not provide 100 percent abstraction because of the fact that it can have concrete method.

What is an Interface?

An interface though looks like a class, it is not. Because Java does not support multiple inheritances, an interface sometimes is ideal for providing base for class hierarchy. An interface is more of a blueprint of a class which is also used to achieve abstraction in Java. It contains abstract methods and static constants. In simple terms, an interface is a collection of abstract methods that is used to specify a behavior that the class has to implement. Unlike abstract class, an interface provides full abstraction in Java. It can have both methods and variables just like a class, however the methods declared in interface are abstract by default.

Difference between Abstract Class and Interface in Java

  1. “Multiple Inheritance” of Abstract Class and Interface in Java – Despite the fact that both abstract class and interface are primarily used for abstraction, they are quite different from each other. While an abstract class is a class, an interface is an interface. It means while an interface can only extend another Java interface, an abstract class can implement multiple inheritances in Java. Well, Java does not support multiple inheritances but one can implement multiple inheritances in Java.
  2. “Method Type” of Abstract Class and Interface in Java – The second major difference between the two is that while an interface can only have abstract methods, an abstract class can have both abstract and non-abstract methods. However, since Java 8, an interface can have default and static methods too.
  3. “Access Modifiers” of Abstract Class and Interface in Java – An abstract class can have public, private and protected access modifiers, however methods of an interface are implicitly public and abstract. You cannot use any other access modifier with interface methods’.
  4. “Implementation” of Abstract Class and Interface in Java – An abstract class can provide the implementation of an interface. An interface, on the other hand, cannot provide the implementation of an abstract class.
  5. “Keyword” of Abstract Class and Interface in Java– While “abstract” keyword is used to declare an abstract class method as abstract, however, interfaces are entirely a different story; they can only have public, static final constants and method declarations.
  6. “Use” of Abstract Class and Interface in Java – While an interface is ideal for type declaration, an abstract class is best suited for code reusability and evolution perspective. An interface looks like a class, but it is not a class.
  7. “Reliability” of Abstract Class and Interface in Java– Another major difference between an abstract class and an interface is that the former is a bit faster than the latter because a search is usually involved in an interface before any overridden method is called in Java.
  8. “Final Variables” of Abstract Class and Interface in Java – The variables declared in an interface are final by default, while an abstract class may or may not contain non-final variables.

Abstract Class vs Interface in Java: Comparison Table

Abstract Class Interface
Abstract keyword is used to create an abstract class and it can be used with methods. Interface keyword is used to create an interface but it cannot be used with methods.
A class can extend only one abstract class. A class can implement more than one interface.
An abstract class can have both abstract and non-abstract methods. An interface can have only abstract methods.
Variables are not final by default. It may contain non-final variables. Variables are final by default in an interface.
An abstract class can provide the implementation of an interface. An interface cannot provide the implementation of an abstract class.
It can have methods with implementations. It provides absolute abstraction and cannot have method implementations.
It can have public, private, static and protected access modifiers. The methods are implicitly public and abstract in Java interface.
It doesn’t support multiple inheritances. It supports multiple inheritances.
It is ideal for code reuse and evolution perspective. It is ideal for Type declaration.

Summary points on  Abstract Class and Interface in Java

Both abstract class and interface are used for abstraction; however, they have their fair share of differences. The main difference between the two is driven by abstraction methods. While the former may or may not have abstract methods, the latter is implicitly abstract. An abstract class can have both abstract and non-abstract methods. An interface, on the other hand, can only have abstract methods, thus they cannot have implementation, which simply means an interface cannot have non-abstract methods. An abstract class cannot be instantiated which prohibits creation of an object. An interface, on the other hand, may look like a class, but it is not.

Sharing is caring!


Search DifferenceBetween.net :




Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.


1 Comment

  1. Interface is a keyword interface name can be user defined name the default signature of variable is public static final and for method is public abstract. Many thanks for sharing this.

Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

References :


[0]Bloch, Joshua. Effective Java Programming Language Guide. Boston: Addison-Wesley, 2001. Print

[1]Bloch, Joshua. Effective Java Programming Language Guide. Boston: Addison-Wesley, 2001. Print

[2]Flanagan, David. Java in a Nutshell. Sebastopol: O’Reilly Media, 1996. Print

[3]Eckel. Thinking in Java. Delhi: Pearson Education India, 2006. Print

[4]"Image Credit: https://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class"

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about : ,
Protected by Copyscape Plagiarism Finder