Abstract Classes and Interfaces – Similarities and Differences

An abstract class may or may not contain concrete functions and may or may not contain abstract functions.
An interface can contain only abstract methods.
You cannot create instances of interfaces or abstract classes.
All variables declared in an interface are presumed to be as public static final even if not explicitly declared as such. That is the value contained in variables in an interface cannot be altered.

What is the purpose of using abstract classes when you have interfaces?

A class can implement multiple interfaces while a class can extend only one concrete or abstract class. Thus interfaces allow a variation of multiple inheritance, though not exactly in the proper sense.

In an abstract class you put all the common functionality that you intend your subclasses to inherit (Inheritance of functionality).
An interface guarantees that all the classes that implement it, provide concrete implementations of its abstract methods, if not, that class itself is to be declared as abstract (Inheritance of type).

Note: An interface can extend another interface. An abstract class can extend another abstract class. An abstract class can implement interface(s). A concrete class can extend another class or implement interface(s).

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: