Home > Java > javaTutorial > body text

How to use the Java keyword abstract

王林
Release: 2023-05-11 15:01:06
forward
1945 people have browsed it

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();
Copy after login

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("学生应该呼吸新鲜的没有雾霾的空气");
	}
}
Copy after login

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方法");
    } 
}
Copy after login

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() {
	}	
}
Copy after login

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);
            } 
          } 
    }
    Copy after login

    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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template