1. Understanding
abstract
: abstract
2. Function
abstract
Can be used to modify classes and methods.You cannot use
abstract
to modify variables, code blocks, and constructors.You cannot use
abstract
to modify private methods, static methods,final
methods, andfinal
classes.
3. Modified class - abstract class
There must be a constructor in the abstract class to facilitate calling when the subclass is instantiated (involving: subclass The entire process of class object instantiation).
During development, subclasses of abstract classes will be provided to instantiate subclass objects and complete related operations.
Abstract classes cannot be instantiated. Abstract classes are intended to be inherited. Subclasses of abstract classes must override the abstract methods of the parent class and provide a method body. If all abstract methods are not rewritten, it is still an abstract class.
4. Modified method - abstract method
Abstract method only has the declaration of the method, but no implementation of the method. End with a semicolon.
public abstract void talk();
Classes containing abstract methods must be declared as abstract classes. On the contrary, there can be no abstract methods in abstract classes.
This subclass can only be instantiated if the subclass overrides all abstract methods in the parent class.
If the subclass does not override all abstract methods in the parent class, then the subclass is also an abstract class and needs to be modified with
abstract
.
5. Code demonstration
public class AbstractTest { public static void main(String[] args) { //一旦Person类抽象了,就不可实例化 // Person p1 = new Person(); // p1.eat(); } } abstract class Creature{ public abstract void breath(); } abstract class Person extends Creature{ String name; int age; public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } //不是抽象方法: // public void eat(){ // // } //抽象方法 public abstract void eat(); public void walk(){ System.out.println("人走路"); } } class Student extends Person{ public Student(String name,int age){ super(name,age); } public Student(){ } public void eat(){ System.out.println("学生多吃有营养的食物"); } @Override public void breath() { System.out.println("学生应该呼吸新鲜的没有雾霾的空气"); } }
6. Classic question
public class Test1 { public static void main(String args[]) { A a = new B(); a.m1();//B类中定义的m1方法 a.m2();//A类中定义的m2方法 } } abstract class A { abstract void m1(); public void m2() { System.out.println("A类中定义的m2方法"); } } class B extends A { void m1() { System.out.println("B类中定义的m1方法"); } }
7. Anonymous subclass of abstract class
public class PersonTest { public static void main(String[] args) { //匿名对象 method(new Student()); //非匿名的类非匿名的对象 Worker worker = new Worker(); method1(worker); //非匿名的类匿名的对象 method1(new Worker()); //创建了一匿名子类的对象:p Person p = new Person(){ @Override public void eat() { System.out.println("吃东西"); } @Override public void breath() { System.out.println("好好呼吸"); } }; method1(p); //创建匿名子类的匿名对象 method1(new Person(){ @Override public void eat() { System.out.println("吃好吃东西"); } @Override public void breath() { System.out.println("好好呼吸新鲜空气"); } }); } public static void method1(Person p){ p.eat(); p.breath(); } public static void method(Student s){ } } class Worker extends Person{ @Override public void eat() { } @Override public void breath() { } }
8 . Application - Template Method Design Pattern (TemplateMethod)
Abstract classes embody the design of a template pattern. Abstract classes serve as common templates for multiple subclasses, and subclasses are expanded and transformed on the basis of abstract classes. , but the subclass will generally retain the behavior of the abstract class.
When part of the internal implementation of the function is certain, part of the implementation is uncertain. At this time, you can expose the uncertain parts and let the subclasses implement them.
In other words, when implementing an algorithm in software development, the overall steps are very fixed and common, and these steps have already been written in the parent class. However, some parts are volatile, and the volatile parts can be abstracted and implemented by different subclasses. This is a template pattern.
The template method design pattern is a pattern often used in programming. It has its shadow in every framework and class library. For example, the common ones are:
Database access encapsulation;
-
doGet/doPostJunit
unit test; About the method call in - Servlet
of
##JavaWeb
;
Template program in Hibernate
;
Spring
in
JDBCTemlate,
HibernateTemplate, etc. ;
abstract class Template { public final void getTime() { long start = System.currentTimeMillis(); code(); long end = System.currentTimeMillis(); System.out.println("执行时间是:" + (end - start)); } public abstract void code(); } class SubTemplate extends Template { public void code() { for (int i = 0; i < 10000; i++) { System.out.println(i); } } }
The above is the detailed content of How to use the Java keyword abstract. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft