Knowledge 1:
It’s possible to make a class abstract without including any abstract methods. This is useful when you’ve got a class where abstract methods don’t make sense, and yet you want to prevent any instances of that class.
// interfaces/AbstractWithoutAbstracts.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
abstract class Basic3 {
int f() {
return 111;
}
// No abstract methods
}
public class AbstractWithoutAbstracts {
// Basic3 b3 = new Basic3();// [1]
// error: Basic3 is abstract; cannot be instantiated
}
Uncomment [1] and compile AbstractWithoutAbstracts, the error is:
$ javac interfaces/AbstractWithoutAbstracts.java
interfaces/AbstractWithoutAbstracts.java:14: error: Basic3 is abstract; cannot be instantiated
Basic3 b3 = new Basic3();
^
1 error
Knowledge 2:
// interfaces/Instantiable.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
abstract class Uninstantiable {
abstract void f();
abstract int g();
}
public class Instantiable extends Uninstantiable {
@Override
void f() {
System.out.println("f()");
}
@Override
int g() {
return 22;
}
public static void main(String[] args) {
Uninstantiable ui = new Instantiable();
}
}
Note the use of @Override . Without this annotation, if you don’t provide the exact method name or signature, the abstract mechanism sees you haven’t implemented the abstract method and produces a compile-time error.
fox example, when remove method g() in Instantiable class, the error is:
interfaces/Instantiable.java:12: error: Instantiable is not abstract and does not override abstract method g() in Uninstant
iable
public class Instantiable extends Uninstantiable {
^
1 error
or replace method g() to g(int i) and remove its @Override, the error is:
nterfaces/Instantiable.java:12: error: Instantiable is not abstract and does n
ot override abstract method g() in Uninstantiable
public class Instantiable extends Uninstantiable {
^
1 error
when only replace method g() to g(int i), the error is:
interfaces/Instantiable.java:12: error: Instantiable is not abstract and does not override abstract method g() in Uninstant
iable
public class Instantiable extends Uninstantiable {
^
interfaces/Instantiable.java:18: error: method does not override or implement a method from a supertype
@Override
^
2 errors
Knowledge 3: Remember that the defacto access is “friendly.”
In fact, interfaces only allow public methods and if you don’t provide an access specifier, the resulting method is not “friendly,” but public . Whereas abstract classes allow almost everything:
// interfaces/AbstractAccess.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
abstract class AbstractAccess {
private void m1() {}
// private abstract void m1a(); // illegal
protected void m2() {}
protected abstract void m2a();
void m3() {}
abstract void m3a();
public void m4() {}
public abstract void m4a();
}
It makes sense that private abstract is not allowed, because you could never legally provide a definition in any subclass of AbstractAccess.
Knowledge 4:
Abstract classes are also useful refactoring tools, since they allow you to easily move common methods up the inheritance hierarchy. details see here.
referecnes:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/AbstractWithoutAbstracts.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/Instantiable.java
4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/AbstractAccess.java