search
HomeJavajavaTutorialHow to use the Java keyword abstract
How to use the Java keyword abstractMay 11, 2023 pm 03:01 PM
javaabstract

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, and final 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;

  • Junit unit test; About the

    doGet/doPost
  • 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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

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

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

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

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

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

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

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

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

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

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

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

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

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

一文掌握Java8新特性Stream流的概念和使用一文掌握Java8新特性Stream流的概念和使用Jun 23, 2022 pm 12:03 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

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

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft