Java Interface And Abstract Class

Java Interface

  • an interface is implicitly abstract, so you do not need to use abstract keyword while declaring interface
  • all fields are automatically public, static and final
  • as of java 8, interface can define default method and static method, other methods are implicitly abstract, so the abstract modifier is unnecessary
  • methods in interface are implicitly public, cannot be other modifier
  • it can be declared public or package scope(no access modifier)
  • interface only can extends interface(one or more)
  • subclass needs to provide unimplementation methods

Checked exceptions should not be declared on implementations methods other than the ones declared by the interface method or subclasses of those declared by the interface method.

Tagging interface

An interface with no methods in it is refered to as a tagging interface, e.g.

public interface EventListener {}

There are two basic design purposes:

  1. creates a common parent - as with the EventListener interface, which is extended by dozens of other interfaces in the java api you can use a tagging interface to create a common parent among a group of interfaces, for example, when an interface extends EventListener, the JVM knows that this particular interface is going to be used in an event delegation scenario
  2. adds a data type to a class - this situtaion is where the term, tagging comes from. A class that implements a tagging interface does not need to define any methods, but the class becomes an interface type through polymorphism.

Java Abstract Class

Abstract class similar normal class except that:

  • it has abstract keyword in class declaration
  • it may or may not include abstract methods
  • it cannot be instantiated
  • if a class includes abstract method, then this class must be declared abstract
  • it can implement interface without providing the implementation of interface methods
  • subclass needs to provide implementation for all of the abstract methods, if not, then this subclass must also be declared abstract

Comparison

A relationship between a class and an abstract class is of type “is-a” A relationship between a class and an interface is of type “has-a” or can do relation So when you have an abstract class Mammals, a subclass Human, and an interface Driving, then you can say

each Human is-a Mammal each Human has-a Driving (behavior)

In other words, abstract class is for related classes, interface is for unrelated classes.

Before Java 8, if subclasses needs to share common code, then developer need to use abstract class instead interface.

There are some quotation from web:

if you don’t know all the implementation details of your super class, you can’t be sure that your subclass is working correctly. I’m pretty sure you have extended from abstract classes and have implemented the missing abstract methods before but have you ever checked the source code of the super class to be sure that your implementation of those abstract methods are as intended by the author of the super class? By the way, the source code of a super class could have been changed with every update. You might have to look at the source code again to verify that your subclass implementation still conforms with the intention of the super class.

Implementing an interface consumes very little CPU, because it’s not a class, just a bunch of names, and therefore there isn’t any expensive look-up to do. It’s great when it matters, such as in embedded devices. Abstract classes, unlike interfaces, are classes. They are more expensive to use, because there is a look-up to do when you inherit from them. On some dynamic languages, if you use an interface, the interpretter will just check if the class complies to the implementation. On abstract classes, it will setup the whole inheritence mecanisme, including what you need to find out the Method Resolution Order. If you create a lot of objects, it’s an overhead. Not a bit one, but depending of your requirements, you may want to take it into considerations.

Written on October 25, 2017