Java Made Clear: Difference between Abstract class Interface

Java Made Clear: Difference between Abstract class Interface

Difference Abstract class Interface

Summary: By the end of this tutorial" Difference Abstract class Interface", you will be comfortable to their differences.

Introduction

A novice feels why both abstract class and interface are required in Java as an abstract class can contain all abstract methods also. The main difference just on the face is Interface gives multiple inheritance facility which basically Java does not.

When to go for abstract class and when to go for interface is a basic decision to be taken by an Architect at the designing part of the product (and not at Programmer level). If the Designer would like partial implementation (body or functionality for a few methods) of the class, he prefers abstract class. He prefers interface when no implementation (all methods abstract) should be given.

A) List showing differences

Note: To get good command over Java, write small programs on the following differences and check whether true not.

Property Abstract classs Interface
Inheritance types Two types are supported Multilevel, Hierarchical and does not Multiple Supports all types – Multilevel, Hierarchical and Multiple
Presence of concrete methods Can have concrete (non-abstract) methods also Permits only abstract methods
Inheritance keyword extends implements
Access specifier constraint Abstract method can be public, protected, default and not privte Must be public only
Default nature of methods public (or other specifier) and abstract should be mentioned in method declaration If not mentionsed, JVM takes by default as all the methods must be public and abstract only
Default nature of variables Variables can be private also apart other specifers Variables should be public, final and static and if not given, taken by default
Implementation Few methods can be implemented (given body) No method should have implementation (or no method should have body)
No of super classes Abstract class can extend only one abstract class or one concrete class Can extend any number of interfaces
Presence of constructors Constructors (also overloaded) can exist Constructors should not exist
Presence of main() Can have main() method Cannot have main() method
B) Similarities (or common features)
  1. With abstract classes and interfaces, object creation is not permitted but creation of reference variables is permitted.
  2. With the both, we can have dynamic binding and dynamic polymorphism.
Following code also works:

1. For more on abstract classes read: Abstract classes

2. For more on interfaces read: Interfaces – Partial implementation of Multiple Inheritance.

Pass your comments and suggestions to improve the quality of this tutorial "Difference Abstract class Interface".

Leave a Reply

Your email address will not be published. Required fields are marked *