abstract class

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值