Java abstract class and abstract keyword


The content of the article is selected from Shang Silicon Valley, jdk8, eclipse environment

Usage of abstract keyword

The abstract keyword can be used to modify classes and methods.

abstract keyword modification class

The abstract modified class indicates that the class cannot be instantiated. If you want to call the method in the class, let the class be inherited by the subclass, and call the method of the parent class through the subclass.

Code demo

package com.atguigu.java;

public class AbstractTest {
    
    
	public static void main(String[] args) {
    
    
//		Person p1 = new Person();
	}
}

abstract class Person{
    
    
	String name;
	int age;
	
	public Person(){
    
    
		
	}
	
	public Person(String name,int age){
    
    
		this.name = name;
		this.age = age;
	}
	
	public void eat(){
    
    
		System.out.println("人吃饭");
	}
	
	public void walk(){
    
    
		System.out.println("人走路");
	}
}

class Student extends Person{
    
    
	
}

The commented part is the part that failed to compile, because the Person declared as an abstract class cannot be instantiated.

There is also a constructor in an abstract class, and its constructor is used to call subclasses. It can also be used to create anonymous subclasses.

abstract keyword modification method

  • The method modified by the abstract keyword is called an abstract method. Abstract methods have no method body and cannot be called.
  • Because abstract methods cannot be called, it is necessary to declare that the class to which the abstract method belongs is also an abstract class. An abstract class does not necessarily have to have abstract methods. A class with abstract methods must be an abstract class.
  • Abstract methods are also inherited by subclasses. If you want to instantiate a subclass object, you must let the subclass override all the abstract methods of the parent class. The parent class here includes the direct parent class and the indirect parent class. Otherwise the compiler will report an error.
  • If the subclass does not override the abstract method of the parent class, the subclass must also be declared as an abstract class, otherwise the compiler will report an error.

The demo code is as follows

abstract class Person{
    
    
	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("人走路");
	}
}

An abstract method has no method body, and its manifestation is without curly braces.

The abstract method only serves as a declaration in the parent class, telling the instantiated subclass to override the abstract method before it can be used, and the subclass must override all the abstract methods of the direct parent class and the indirect parent class.

public class AbstractTest {
    
    
	public static void main(String[] args) {
    
    
//		Person p1 = new Person();
	}
}

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 void eat(){
    
    
		System.out.println("学生吃饭");
	}

	@Override
	public void breath() {
    
    
		System.out.println("学生呼吸");
	}
}

Note on the use of abstract

The abstract keyword conflicts with the use of several other keywords. Behave as

  • abstract cannot be used to modify private methods, because private methods cannot be overridden.
  • Abstract cannot be used to modify static methods. If there are methods with the same name and the same parameters in the subclass and the parent class, if the methods are non-static, they constitute rewriting. If they are both declared as static, they are not rewritten. This is because static The method cannot be overridden. If only one is static, the compiler reports an error.
  • Abstract cannot be used to modify final methods and final classes. Because final classes cannot be inherited, final methods cannot be overridden.

abstract exercise

Insert picture description here
Picture from Shang Silicon Valley PPT

First, you have to write a parent class of Employee and declare it as an abstract class.

package com.atguigu.exer1;

public abstract class Employee {
    
    
	private int id;
	private String name;
	private double salary;
	public Employee() {
    
    
		super();
	}
	public Employee(int id, String name, double salary) {
    
    
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
	
	public abstract void work();
}

When creating the constructor, directly alt+shift+s to create.

Create another Manager class. When creating a subclass, select the inherited parent class Employee in the eclipse pop-up box, and you can directly get the overridden work method.

package com.atguigu.exer1;

public class Manager extends Employee {
    
    
	private double bonus;
	
	public Manager(double bonus) {
    
    
		super();
		this.bonus = bonus;
	}

	public Manager(int id, String name, double salary, double bonus) {
    
    
		super(id, name, salary);
		this.bonus = bonus;
	}

	@Override
	public void work() {
    
    
		System.out.println("管理");

	}

}

Create a CommonEmployee class

package com.atguigu.exer1;

public class CommonEmployee extends Employee {
    
    
	
	@Override
	public void work() {
    
    
		System.out.println("工作");

	}

	public CommonEmployee(int id, String name, double salary) {
    
    
		super(id, name, salary);
		
	}

	public CommonEmployee() {
    
    
		super();
		// TODO Auto-generated constructor stub
	}

}

Retest

package com.atguigu.exer1;

public class EmployeeTest {
    
    
	public static void main(String[] args) {
    
    
		Employee manager = new Manager(1001,"库克",100,1000);
		manager.work();
		CommonEmployee commonemployee = new CommonEmployee();
		commonemployee.work();
	}
}

The result is

Management
work

This is a simple application of an abstract class.

Create an anonymous subclass object of the abstract class

Use of anonymous objects

First look at the use of anonymous objects, create a PersonTest class in the previous AbstractTest package

package com.atguigu.java;

public class PersonTest {
    
    
	public static void main(String[] args) {
    
    
		
		method(new Student());

	}
	
	public static void method(Student s){
    
    
		
	}
}

The above is manifested as the use of an anonymous object. But the class is not anonymous.

Non-anonymous objects of non-anonymous classes

This is normal writing.

package com.atguigu.java;

public class PersonTest {
    
    
	public static void main(String[] args) {
    
    
		
		method(new Student());
		Worker worker = new Worker();
		method1(worker);//非匿名的非匿名的对象

	}
	
	public static void method(Student s){
    
    
		
	}
	
	public static void method1(Person p){
    
    
		p.eat();
		p.breath();
	}
}

class Worker extends Person{
    
    

	@Override
	public void eat() {
    
    
		// TODO Auto-generated method stub
		
	}

	@Override
	public void breath() {
    
    
		// TODO Auto-generated method stub
		
	}
	
}

If written as

		method1(new Worker());//非匿名类的匿名对象

It is an anonymous object of a non-anonymous class. The name of the object is not known, but the class to which the object belongs is known.

Anonymous subclass

Create an anonymous class in the main method

	public static void main(String[] args) {
    
    
		
		method(new Student());
		Worker worker = new Worker();
		method1(worker);//非匿名类的非匿名的对象
		method1(new Worker());//非匿名类的匿名对象
		//匿名类的非匿名对象
		Person p1 = new Person(){
    
    

			@Override
			public void eat() {
    
    
				System.out.println("eat");
				
			}

			@Override
			public void breath() {
    
    
				System.out.println("breath");
				
			}
			
		};

	}

An object of an anonymous class is created here, the object is p1, but the class name is unknown, so it is called an anonymous class. Person p1 = new Person(){overridden method}; here the abstract method of the parent class is overridden inside the braces, and the content of the overridden method is arbitrary, so it is a new subclass without a class name, and becomes Anonymous class. This can also be seen as a use of polymorphism.
Allowed in the main method

		method1(p1);

The result is

eat
breath

Anonymous objects of anonymous subclasses

You can also use anonymous objects of anonymous classes

		method1(new Person(){
    
    

			@Override
			public void eat() {
    
    
				System.out.println("eat eat");
				
			}

			@Override
			public void breath() {
    
    
				System.out.println("breath breath");
				
			}
			
		});

The result is

eat eat
breath breath

Note that there is a difference between the semicolon and the semicolon of the non-anonymous objects of the anonymous class.

Guess you like

Origin blog.csdn.net/Meloneating/article/details/114176772