abstract

Abstract Class

  • An abstract class is a class that is declared abstract—it may or may not include abstract methods.
  • An abstract class can contain abstract methods that must be implemented in non-abstract subclasses
  • It can contain fields and non-abstract methods (including static)
  • An abstract class can extend another class, including an abstract one
  • It’s impossible to create an instance of an abstract class but it can contain a constructor.
  • An abstract class may have static fields and static methods.
    • You can use these static members with a class reference (for example, AbstractClass.staticMethod()) as you would with any other class.

Abstract Method

  • An abstract method is a method that is declared without an implementation.
  • If a class includes abstract methods, then the class itself must be declared abstract.
  • When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
  • Note: static or default methods can’t be abstract!
  • It is a compile-time error if a method declaration that contains the keyword abstract also contains any one of the keywords:
    • private, static or final
    • nativestrictfp or synchronized

Abstract Classes Compared to Interfaces

  • Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation.
  • Methods:
    • An abstract class can have abstract and non-abstract instance methods while an interface can have abstract or default instance methods;
    • Both can have static methods (Note: static or default methods cannot be abstract)
    • With abstract classes, you can define public, protected, and private concrete methods.
    • With interfaces, all methods are public by default and you can only define private method
  • Fields:
    • With interfaces, all fields are automatically public, static, and final
    • With abstract classes, you can declare static, non-static, final, non-final fields
  • Inheritance:
    • An abstract class can extend another abstract or regular class and an interface can only extend another interface;
    • An abstract class can extend only one class while an interface can extend any number of interfaces;
    • An abstract class can provide an implementation of an interface but an interface cannot provide an implementation of an abstract class;
    • An abstract class can have a constructor and an interface cannot;
    • Note:
      • a class extends another class
      • a class implements an interface
      • an interface extends another interface
  • In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
  • Check interface

Which should you use, abstract classes or interfaces?

  • Consider using abstract classes if any of these statements apply to your situation:
    • You want to share code among several closely related classes.
    • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
    • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
  • Consider using interfaces if any of these statements apply to your situation:
    • You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
    • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
    • You want to take advantage of multiple inheritance of type.
    • An example of an abstract class in the JDK is AbstractMap, from Collections.
      • Subclasses: HashMap, TreeMap, and ConcurrentHashMap
      • Methods shared: get, put, isEmpty, containsKey, and containsValue
  • All abstract classes are not pure abstract, partially they are. So if you need a pure abstract class you may go for an Interface.

When an Abstract Class Implements an Interface

  • Generally a class that implements an interface must implement all of the interface’s methods.
  • It is possible, however, to define a class that does not implement all of the interface’s methods, provided that the class is declared to be abstract.
  • For example, In this case, class X must be abstract because it does not fully implement Y, but class XX does, in fact, implement Y.
abstract class X implements Y {
  // implements all but one method of Y
}
 
class XX extends X {
  // implements the remaining method in Y
}

Constructor

  • An abstract class has constructor. A class in general has a constructor so that when an instance of the class is created, the fields of the class can be setup to a initial valid state.
  • While interfaces cannot contain constructor
  • Why there is constructor at all?
    • Abstract classes define partial implementation of a public contract. Which means that it may implement some of the methods and contains partially implemented methods that are marked abstract.
    • Since abstract classes can have partial implementation and such partial implementation can include fields of the class, a constructor becomes necessary so that those fields are initialized to a valid default state when they are created thru the constructor of their concrete subclasses.

Override default method as an abstract method