Detailed explanation of java abstract classes and abstract methods + Exercises

Abstract classes and abstract methods

1, Why use abstract methods and abstract classes

2, Abstract method
Abstract methods refer to methods that have only method declarations but no concrete method bodies.

structure
abstract class Class name // Define abstract classes
{
    Declare data members;
    Data type method name (parameter) of the return value of the access right...)//Define general methods
    {
        ...
    }
    abstract Data type and method name (parameter) of the return value...);
    //Define an abstract method. In an abstract method, there is no method body defined
}
**be careful:**

 ordinary methods must have a method body, and abstract methods cannot have a method body (there are no braces)
 abstract methods should be modified with abstract
 the abstract method has no method body and ends directly with a semicolon.
public abstract void shout();
public abstract void eat();

3, Abstract class
Concept: classes containing abstract methods are called abstract classes. Abstract classes provide a specification for subclasses. The purpose is to let subclasses inherit.
Structure: modifier abstract class class name {}
You can define: common variables, abstract methods, construction methods, and common methods
be careful:
 if no subclass inherits extensions, this specification has no meaning.
 definition method of abstract class keyword Abstract
 if a class contains abstract methods, the class must be abstract.

Example: public abstract class Animal {}
Abstract method shot eat

4, Implementation of abstract methods
Abstract methods must be implemented by inheriting extensions. Subclasses that inherit abstract classes must override all abstract methods of the parent class. Otherwise, the subclass must also be declared as an abstract class.
Keywords: Extensions
Example 01: (1) define an abstract class Animal, including name and age attributes,
A common method toString() has an abstract method shoot(), eat(), and a common method sleep(), play()
(2) Define a Dog class, inherit the abstract class Animal, and implement the abstract method
(3) Define a Cat class, inherit the abstract class Animal, and implement the abstract method
(4) Define a Tiger class, inherit the abstract class Animal, and implement the abstract method
(5) Define a Goat class, inherit the abstract class Animal, and implement the abstract method
(6) Define a test class, create the above animals, and print out

5, Attention
The usage principles of abstract classes are as follows:
 the abstract method must be public or protected, and the abstract class defaults to the public definition keyword abstract
 abstract methods cannot be instantiated directly and need to be inherited by subclasses using the keyword extends
 a subclass can only inherit one abstract class
 if the subclass is not an abstract class, all abstract methods in the abstract class must be overridden (if the subclass does not implement the abstract method of the parent class, the subclass must also be defined as an abstract class)
 abstract classes cannot use the final keyword, because abstract classes must have subclasses, while final defined classes cannot have subclasses

6, Summary
 1. Abstract classes cannot be instantiated. If they are instantiated, an error will be reported and the compilation cannot pass.
 2. Only non Abstract subclasses of abstract classes can create objects
 3. Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be defined as abstract classes
 4. The abstract method in the abstract class is only a declaration, not a method body.
 5. The subclass of an abstract class must give the concrete implementation of the abstract method in the abstract class, unless the subclass is also an abstract class.
7, Exercises

package case5;

public class abstractTest {
	public static void main(String[] args) {
		/********* begin *********/
		// Instantiate the objects of Student class and Worker class respectively, and call their respective construction methods to initialize class properties.
		Student s=new Student("Zhang San",20,"student");
        Worker w=new Worker("Li Si",30,"worker");
        System.out.println( s.talk());
        System.out.println( w.talk());
		// Call the copied talk() method in their respective classes to print information.
		
		/********* end *********/
		
	}
}

// Declare an abstract class named Person. In Person, declare three attributes name age occupation and an abstract method - talk().
abstract class Person {
	/********* begin *********/
String name;
int age;
String occupation;
abstract String talk();	/********* end *********/
}

// The Student class inherits from the Person class, adds a constructor with three parameters, and duplicates the talk() method to return name, age and occupation information
class Student extends Person {
	/********* begin *********/
// public Student(){

// }
public Student(String name,int age,String occupation){
   this.name=name;
   this.age=age;
   this.occupation=occupation;
}
 String talk(){
    return "Student——>full name:"+name+",Age:"+age+",occupation:"+occupation+"!";
}
	/********* end *********/
}

// The Worker class inherits from the Person class, adds a constructor with three parameters, and the duplicate talk() method returns name, age and occupation information
class Worker extends Person {
	/********* begin *********/
// public Worker(){

// }
public Worker(String name,int age,String occupation){
   this.name=name;
   this.age=age;
   this.occupation=occupation;
}
 String talk(){
     return "Workers——>full name:"+name+",Age:"+age+",occupation:"+occupation+"!";
}
	/********* end *********/

}

Posted on Tue, 30 Nov 2021 11:54:13 -0500 by truckie2