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 modifying a class with abstract, this class is called an abstract class, and the method is called an abstract method when it is used to modify a method with abstract. For example:
Abstract class:
Abstract class Animal {//using abstract to define a Animal as an abstract class}
Abstract method:
public abstract void enjoy (); To define an abstract method "enjoy" with abstract
2 classes containing abstract methods must be declared as abstract classes, abstract classes must be inherited, and abstract methods 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:
Abstract method public abstract void enjoy ();//This abstract method does not need to implement function//general method public void enjoy () {System.out.print ("Scream"); And the general approach is necessary to implement certain functions}
Example A: polymorphic programs with no abstract classes
Class Person {public void F () { System.out.println ("parent Class");} } Class Students extends person {public void F () { System.out.println ("Student Class");} } Class Teacher extends person {public void F () { System.out.println ("Teacher Class");} } public class Test3 {public static void Main (string[] args) {person s = new Students (); Person T = new Teacher (); S.F (); T.f (); }}
Example B: polymorphic programs for abstract classes
Abstract class Person {public abstract void f ();} Class Students extends person {public void F () { System.out.println ("Student Class");} } Class Teacher extends person {public void F () { System.out.println ("Teacher Class");} } public class Test3 {public static void Main (string[] args) {person s = new Students (); Person T = new Teacher (); S.F (); T.f (); }}
The difference between abstract classes and abstract methods in Java