Abstract method: A method that does not have a method body in a class is an abstract method. Abstract classes: Classes that contain abstract methods are called abstract classes. Abstract methods in abstract classes must be implemented! If a subclass does not implement an abstract method in the parent class, the subclass becomes an abstract class! The normal method in an abstract class can be implemented without having to implement it. 1 when you use abstract to modify a class, this class is called an abstract class, and when you use abstract to modify a method, the method is called an abstract method. For example: 1abstract class Animal {//Use abstract to define a Animal as abstract class 2}3 and 4public abstract void enjoy (); To define an abstract method using abstract "Enjoy" 2 the class containing the abstract method must be declared as an abstract class, the abstract class must be inherited, and the abstract method must be overridden. 3 abstract classes cannot be instantiated. 4 abstract methods need to be declared without the need to implement certain functions. such as: 1public abstract void enjoy ();//This abstract method does not need to implement the function 2 3public void enjoy () {4system.out.print ("scream");//And the general method is necessary to implement some functions 5} Example A: Polymorphic program without abstract class 01class person {System.out.println-public void F () {}05}06 ("parent class"); 07class Students Extend s person {The public void F () {System.out.println ("Student Class");}11}12 13class Teacher extends person {+ PU Blic void F () {System.out.println ("Teacher Class"),}17}18 19public class Test3 {public static void main (String [] args) {The person s = new Students (); the person t = new Teacher (); s.f (); T.f (); 25}26 } Example B: Polymorphic program of abstract class 01abstract class Person {Lic abstract void f (); 03}04 05class Students extends person {The public void F () {System.out.println ("Student Class"); 08 }09}10 11class Teacher extends person {a public void F () {System.out.println ("Teacher Class");}15}16 17publi C class Test3 {public static void main (string[] args) {The person s = new Students (); the person t = new Teacher (); s.f (); T.f (); 23}24}
The difference between abstract classes and abstract methods