Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Abstract classes can provide <em>default</em> implementations.</p> <p>Consider the <code>MouseListener</code> in the Java API, and the corresponding abstract class <code>MouseAdapter</code>.</p> <p>Using "pure" (non-abstract) <code>MouseAdapter</code> does not make sense: all methods are implemented as no-ops. However, the class is very convenient to use as abstract parent for a concrete listener, because you only have to <em>override</em> the one method that you are interested in, instead of having to reimplemet lots of methods as no-ops.</p> <p>It's not necessary to make <code>MouseAdapter</code> abstract. It is a complete class. It just doesn't make <em>sense</em> to use it without overriding at least one method. But otherwise, you would need to have <code>MouseListenerWithoutMouseClicked</code>, if you wanted it to have at least one <code>abstract</code> method.</p> <p>Another example would be the <a href="https://en.wikipedia.org/wiki/State_pattern" rel="nofollow">state pattern</a>. The interface defines the actual API. The abstract class would implement (some or all) transition functions as the <em>default operation</em>. Each state would only override those methods, where it diverts from the default behavior.</p> <p>In general, any "abstract" method declaration <em>could</em> (and often: should) be moved to an interface instead. The only reason why I often declare them as <code>@Override abstract</code> nevertheless is documentation purposes, to emphasize which methods need to be implemented for a concrete instance. Any method inherited from an interface will be effectively declare an abstract method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload