知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

      • Theory:Introduction OOP
      • Theory:Defining classes
      • Theory:Instance methods
      • Thoery:Constructor
      • Theory:Package
      • Theory:Access modifiers
      • Theory:Getters and setters
      • Theory:Inheritance
      • Theory:Protected modifier
      • Theory:Referencing subclass objects
      • Theory:The Object class
      • Theory:Objects
      • Theory:Interface
      • Theory:Multiple constructors
      • Theory:The keyword super
      • Theory:Static members
      • Theory:Hiding and overriding
      • Theory:Polymorphism
      • Theory:Abstract class
        • What is an abstract class?
        • Example
      • Theory:Abstract class vs interface
      • Theory:Anonymous classes
      • Theory:Anonymous classes properties
    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java OOP
Jim
2022-09-12
目录

Theory:Abstract class

Sometimes you have a set of fields and methods that you need to reuse in all classes within a hierarchy. It is possible to put all the common members to a special base class and then declare subclasses which can access these members. At the same time, you do not need to create objects of the base class. To achieve it, you can use an abstract class as the base class in the hierarchy.

# What is an abstract class?

An abstract class is a class declared with the keyword abstract. It represents an abstract concept that is used as a base class for subclasses.

Abstract classes have some special features:

  • it's impossible to create an instance of an abstract class;
  • 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 abstract;
  • it can contain a constructor.

As you can see, an abstract class has two main differences from regular (concrete) classes: no instances and abstract methods.

注意

Abstract methods are declared by adding the keyword abstract. They have a declaration (modifiers, a return type, and a signature) but don't have an implementation. Each concrete (non-abstract) subclass must implement these methods.

提示

Note, static methods can't be abstract!

# Example

Here is an abstract class Pet:

public abstract class Pet {

    protected String name;
    protected int age;

    protected Pet(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public abstract void say(); // an abstract method
}
1
2
3
4
5
6
7
8
9
10
11
12

The class has two fields, a constructor, and an abstract method.

Since Pet is an abstract class we cannot create instances of this class:

Pet pet = new Pet("Unnamed", 5); // this throws a compile time error
1

The method say() is declared abstract because, at this level of abstraction, its implementation is unknown. Concrete subclasses of the class Pet should have an implementation of this method.

Below are two concrete subclasses of Pet. You can see that they override the abstract method:

class Cat extends Pet {

    // It can have additional fields as well

    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void say() {
        System.out.println("Meow!");
    }
}

class Dog extends Pet {

    // It can have additional fields as well

    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public void say() {
        System.out.println("Woof!");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

We can create instances of these classes and call the say() method.

Dog dog = new Dog("Boss", 5);
Cat cat = new Cat("Tiger", 2);
        
dog.say(); // it prints "Woof!"
cat.say(); // it prints "Meow!"
1
2
3
4
5

Do not forget that Java doesn't support multiple inheritance for classes. Therefore, a class can extend only one abstract class.

编辑 (opens new window)
#Java#OOP#Abstraction
上次更新: 2022/10/25, 17:57:56
Theory:Polymorphism
Theory:Abstract class vs interface

← Theory:Polymorphism Theory:Abstract class vs interface→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式