The grammatical characteristics of abstract functions
Syntax characteristics of abstract classes
The role of abstract classes
Abstract classes are used to be inherited.
1. What is abstract function
Only the function is defined, the function without the function body is called abstract function;
abstract void fun ();
2, what is abstract class (commonly referred to as the base class)
Abstract class person{}
Classes defined using abstract are referred to as abstract classes;
(1) Abstract classes are not able to generate objects;
(2) If a class contains abstract functions, then the class must be declared as abstract class;
(3) If there is no abstract function in a class, then the class can also be declared as an abstract class. (sometimes let this class not produce objects)
3. Can the abstract class have a constructor function?
Yes
Abstract class person{ String name; int Age ; Person () { System.out.print ("constructor for person"); } void introduce () { System.out.print (name+ " " + Age); } Abstract void
class extends person{ Chinese () {// generate subclasses must first call the constructor of the parent class super (); System.out.print ("Chinese constructor"); } // the abstract function in the replication person abstract class void eat () { System.out.print ("Eat with Chopsticks");} }
class test{ publicstaticvoid main (String args[]) { new Chinese (); P.eat (); }}
Why use abstract classes?
There are many features
For example, after defining an abstract class, let subclasses inheriting this abstract class must implement abstract functions in the abstract class.