Java Burning Interview question – Interface vs Abstract classes, a practical difference
An Interface: This is a collection of methods. They have no definition whatsoever and their function is determined by the class that implements them. An example of an interface is a List. All lists (ArrayList, LinkedList) have add() and remove() methods because they implement the List interface, which demands them.
public interface List {
public void add (Object o);
public void remove (Object o);
}
public class MyList implements List {
public void add (Object o) {
// I must implement this method because of the interface List
}
public void remove (Object o) {
// I must implement this method because of the interface List
}
}
The implements keyword is used by a class to indicate that it is going to implement the methods demanded by an interface. A class can implement as many interfaces as it likes; the only requirement is that it provides a definition for each of those methods. It can also be defined as abstract and rely upon its subclasses to define some. Then, those methods are just like the abstract methods of any other abstract class.
Take an example of List interface. Most methods don’t care what kind of list they get; they just want to know that the object supports the common list methods. In the statement:
List<String> myList = new ArrayList();
You are creating an actual ArrayList object, but hiding it’s implementation under the List interface. Then, later, if you decide to use a LinkedList instead, you don’t have to change all of your code since it is also implementing the List interface.
Abstract class is a partially implemented class and it has no real meaning other than serving as a parent for multiple child classes those with real meaning. Abstract class is special parent class that provides default functionalities to multiple child classes. It is created as abstract because of unavailability of suitable concrete parent class.
The extends keyword is used by a class to indicate that it is going to add functionality to some existing class. If the parent class is abstract, the extending class must implement any of those abstract methods in the parent.
Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
Points to remember:
Interfaces and interface methods are implicitly abstract even if not declared as so. So there is no need to explicitly specify it.
Below code is senseless.
public abstract interface <interface-name> {
public abstract void <function-name>(...);
}
Other differences
- An interface can extend any number of interfaces.
- A class can optionally extend exactly one class; it extends Object if you don’t specify anything.
- A class can implement any number of interfaces.
- Any class can be declared abstract.
- Any class that has abstract methods must be declared abstract.
- Any class that extends an abstract class or implements an interface must implement all abstract and interface methods or else must be declared abstract.
Practical usages and designs to inscribe clear picture in your mind
- Interfaces allow the creation of proxies that encapsulate a concrete class. This is used extensively by frameworks in order to intercept method calls to the concrete class (e.g., for starting a transaction before the method is executed or to write to the log).
- Keep watch over upload file size
- Creating progress monitor in Java
- If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
I am designing an java based API. I’ll share its design once it is completed. It’ll help you to understand the concepts of interfaces, abstract classes, enum, modifers, creating your own data type, delegators, OOPS concept etc. with example. So keep reading….
views


ALi
13 Mar, 2012
Good work
kindly share your designing an java based API once completd
Amit Gupta
15 Mar, 2012
I have left idea to develop that application for some time since very busy in seeking job. Mean while you can go with http://article-stack.com/how-why/analysis-of-processing-data-file.amty for some practice