Difference between Interface and Abstract Class in Java

Key Difference: Interface or a protocol is a common way that allows unrelated objects to communicate with each other. It represents an agreed-upon behavior that facilitates the interaction between the two objects. An abstract class is a class of objects that cannot be instantiated or set up an instance of an object. This class may have no implementation or can have incomplete implementation.

 Java, C++, C# are all complicated programming languages that require a detailed study and understanding to be able to work with. There are many different methods that are used in these languages that decide how the language will work. Interface and Abstract Class are two confusing concepts that are part of OOP (object-oriented programming). These two can be used individually or together with each other. These two are completely different from each other and are used in different cases.

Java is a programming language, which has been influenced by the C language. It derives much of its syntax from C and C++, however it has fewer low-level facilities than either. Java is a general-purpose programming language that is designed to have fewer implementation dependencies as compared to previous languages. It is concurrent, class-based, and an object-oriented language.

Java was developed by James Gosling at Sun Microsystems and was released in 1995. It was originally released as a core component of Sun Microsystems' Java platform. Sun has since re-licensed most of its Java technologies under the GNU General Public License. This means that all the technology licensed under GNU General Public License is open source and generally available free of cost. Sun Microsystems eventually merged into Oracle Corporation. As of 2012, Java has become one of the most popular programming languages for use. This is especially true for client-server web applications. Many other systems have developed alternative implementations of Sun technologies, such as GNU Compiler for Java and GNU Classpath.

Interface or a protocol is a common way that allows unrelated objects to communicate with each other. It represents an agreed-upon behavior that facilitates the interaction between the two objects. The interface holds the key to the various actions being implemented and serves as a link between the two different objects. If a class of objects is exposed to the interface, the class would have to follow the behavior implemented by the interface. Commonly, interfaces only contain constant declarations and method signatures. Any interface that implements a particular interface, must apply all methods mentioned in the interface or be declared as an abstract class. In Java, any object can be defined as an interface, if that object is null or holds a class in which it implements that particular interface. One can implement multiple interfaces to a single class in Java using implement keywords.

An abstract class is a class of objects that cannot be instantiated or set up an instance of an object. This class may have no implementation or can have incomplete implementation. It can also have abstract methods or properties that are shared by all the sub-classes. In some programming languages, abstract types with no implementation are known as interfaces. In Java, an abstract type can be created using the keyword ‘abstract’ in the class definition. The purpose of having an abstract class to place all the methods that are not implemented in the abstract class and leave it to the subclass to determine how to implement these classes. If a class has a single abstract method, the class must be declared as an abstract class, although one does not need to have at least one single method to declare a class abstract.

Malliktalksjava.in lists the major differences between interface and abstract as:

  • Abstract class has the constructor, but interface doesn’t.
  • Abstract classes can have implementations for some of its members (Methods), but the interface can’t have implementation for any of its members.
  • Abstract classes should have subclasses else that will be useless.
  • Interfaces must have implementations by other classes else that will be useless
  • Only an interface can extend another interface, but any class can extend an abstract class.
  • All variable in interfaces are final by default
  • Interfaces provide a form of multiple inheritance. A class can extend only one other class.
  • Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
  • A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
  • Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast.
  • Accessibility modifier (Public/Private/internal) is allowed for abstract class. Interface doesn’t allow accessibility modifier
  • An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
  • An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property’s signature but no implementation.
  • Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
  • Abstract scope is up to derived class.
  • Interface scope is up to any level of its inheritance chain.

The information for the table is the courtesy of codeproject.com and mindprod.com

 

Interface

Abstract Class

Multiple inheritance

A class may inherit several interfaces.

A class may inherit only one abstract class.

Default implementation

An interface cannot provide any code, just the signature.

An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modifiers

An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public.

An abstract class can contain access modifiers for the subs, functions, properties.

Core vs. Peripheral

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

An abstract class defines the core identity of a class and there it is used for objects of the same type.

Homogeneity

If various implementations only share method signatures then it is better to use Interfaces.

If various implementations are of the same kind and use common behavior or status then abstract class is better to use.

Speed

Requires more time to find the actual method in the corresponding classes.

Fast

Adding functionality

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Fields and Constants

No fields can be defined in interfaces.

An abstract class can have fields and constants defined.

Terseness

The constant declarations in an interface are all presumed public static final.

Shared code can be added into an abstract class.

Constants

Static final constants only, can use them without qualification in classes that implement the interface.

Both instance and static constants are possible. Both static and instance intialiser code are also possible to compute the constants.

Third Party Convenience

An interface implementation may be added to any existing third party class.

A third party class must be rewritten to extend only from the abstract class.

is-a vs. -able or can-do

Interfaces are often used to describe the peripheral abilities of a class, not its central identity, e.g. An Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.

An abstract class defines the core identity of its descendants. Implemented interfaces enumerate the general things a class can do, not the things a class is.

In a Java context, users should typically implement the Runnable interface rather than extending Thread, because they’re not really interested in providing some new Thread functionality, they normally just want some code to have the capability of running independently. They want to create something that can be run in a thread, not a new kind of thread. The similar is-a vs. has-a debate comes up when you decide to inherit or delegate.

Plug-in

The user can write a new replacement module for an interface that contains not one stick of code in common with the existing implementations. When the user implements the interface, they start from scratch without any default implementation. The user has to obtain tools from other classes; nothing comes with the interface other than a few constants. This gives the user freedom to implement a radically different internal design.

The user must use the abstract class as-is for the code base, with all its attendant baggage, good or bad. The abstract class author has imposed structure on the user.

Maintenance

If the client code talks only in terms of an interface, you can easily change the concrete implementation behind it, using a factory method.

If the client code talks only in terms of an abstract class, you can easily change the concrete implementation behind it, using a factory method.

Image Courtesy: javala.cs.tut.f, mxtutorial.com

Most Searched in Food and Drink Most Searched in Education and References
Most Searched in Environment Most Searched in Electronics
Oxidation vs Reduction
Veneer vs Sunmica
Bitcoin vs Ripple
Plaza vs Piazza

Comments

you are in reality a just right webmaster. The website loading velocity is incredible. It sort of feels that you are doing any unique trick. Moreover, The contents are masterwork. you’ve performed a great process on this topic!

Add new comment

Plain text

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.