Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Working with Abstract Classes and Interfaces in Java

Take advantage of abstract class and interface to design for flexibility in Java.


There are three important constructs in Java — concrete classes, abstract classes and interfaces. An abstract class is a special type of a class that is marked with the abstract keyword to indicate that the class cannot be instantiated but can be inherited if need be. An interface is actually a purely abstract class. We will explore more on both of these as we progress through this article. This article presents a discussion on when and how to use abstract class and interface when working with enterprise applications in Java.

What are Abstract Classes and Interfaces?

An interface doesn't have any state or implementation. It just can have the declaration of methods (method signature). An abstract class on the other hand can have both concrete and non-concrete methods. While an interface is type-centric, a class is implementation-centric. Unlike an interface, an abstract class can optionally have method implementations. An interface cannot have any implementation of the operations it declares. In essence, while an abstract class may or may not have an abstract method, you cannot have any implementation of the operations declared in an interface. An interface cannot have any data members — it can just have declaration of operations. So, an interface just defines a type and a set of operations on that type sans any implementation details. When you create a class that implements an interface, you should define those implementations explicitly.

Choosing between an abstract class and an interface in your designs

The choice between an abstract class and an interface in your design depends on many factors — it is not an either/or proposition. If you would like to design for change, i.e., you would like to design your application such that it can be changed when need be seamlessly, I'd prefer an interface over an abstract class. An interface is a good choice when frequent change in your design is inevitable. An abstract class may be a good choice when you are designing a framework and you would like to have some default operations in your abstract base class. Since Java allows you to implement multiple interfaces but extend just one class, an interface is also helpful when you would typically want to design your class that can implement multiple types.

Another point to consider to choose an interface over an abstract class is the amount of functionality that can be determined upfront and will not change over time. Keeping all such considerations in mind is essential to design applications that can scale and are high performant. The section that follows presents an overview of the points (the choice between abstract class and interface) you should consider when designing enterprise application.

Which one should I opt for when working on Enterprise Applications?

Now, let's discuss some interesting points that are related to using abstract classes and interfaces in enterprise applications. Should I use an abstract class or an interface when designing an enterprise application? Which one is better to use in terms of performance, maintenance and scalability?

I would strongly recommend using interfaces over abstract classes. Also, I prefer composition over inheritance. You should be aware of the fact that interfaces are much simpler to use and are very flexible. Interfaces are also preferred for testability. If you have a collaborator that is declared as an interface, it is easier to test as it can be mocked easily. On the contrary, collaborators that have been declared as an abstract type may be difficult to test and maintain if they contain initialization logic in them.

I would like to point here that Java 8 introduces default methods or defender methods that can be used to add new methods to interfaces without breaking the existing implementation. This is a great new feature that enables you to have an interface that contains a default implementation when one or more classes that implement that interface doesn't have implementation for such operation. Again, given a choice between an abstract class and an interface, I would always put my two cents on the latter for more ways than one.

Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.Net, as well as a speaker and the author of several books and articles. He received the prestigious MVP (Most Valuable Professional) award at ASP.Net for 2007, 2008, 2009, 2010, 2011, and 2012. He has more than 18 years of industry experience in IT, with more than 14 years in Microsoft .Net and its related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times. He is the author of eight books and more than 300 articles. He was a speaker a speaker at the reputed Spark IT 2010 event and at the reputed Dr. Dobb’s Conference 2014 in Bangalore. He is also a regular speaker at SSWUG Virtual Conference. He's also worked as a judge for the Jolt Awards at Dr. Dobb's Journal.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date