In all of our instruments (instrument) examples, the methods within the base class instrument are certainly "pseudo" methods. If you want to call these parties
Error will occur. That is because the intent of instrument is to create a common interface for all classes derived from it.
The only reason to build this generic interface is that it can make different representations for different subtypes. It has created a base for us
This form allows us to define something that is "generic" in all derived classes. To illustrate this idea, another way is to put instrument
Known as the abstract base class (abstract class, for short). If you want to handle a series of classes through this generic interface, you need to create an abstract class. Right
All derived class methods that match the signature of the underlying class declaration can be invoked through a dynamic binding mechanism (however, as noted in the previous section
That way, if the method name is the same as the underlying class, but the arguments or parameters are different, there is an overload, which may not be what we want.
If there is an abstract class like instrument, the object of that class is almost certainly meaningless. In other words, the instrument
Instead of expressing specific implementation details, simply express the interface. So it makes no sense to create a instrument object, and I
Users should always be barred from doing so. For this purpose, all methods within the instrument display an error message. But doing so will
Delay information to run time and require thorough, reliable testing on the user side. Anyway, the best way to do this is to catch it during compilation.
Catch the problem.
In response to this problem, Java provides a mechanism specifically called "abstract methods". It belongs to an incomplete method that contains only one sound
Not a method body. The following syntax is used for abstract method declarations:
abstract void X ();
A class that contains an abstract method is called an abstract class. If a class contains one or more abstract methods, the class must be specified as
Abstract (abstraction). Otherwise, the compiler will report an error message to us.
If an abstract class is incomplete, what action does the compiler take once someone attempts to build an object of that class? Because not
Can safely create an object of its own for an abstract class, so an error message is obtained from the compiler. In this way, the compiler
The "purity" of the abstract class can be guaranteed, and we don't have to worry about misusing it.
If you inherit from an abstract class and want to generate an object of the new type, you must provide a method definition for all the abstract methods in the underlying class.
If you do not (you can choose not to do so), the derived class will also be abstract, and the compiler will force us to use the abstract keyword to mark
The "abstract" nature of the category of ambition.
Even if you do not include any abstract methods, you can declare a class as an "abstract class." If a class does not necessarily have any abstract methods,
And we want to ban all instances of that class, which can be very useful.
The instrument class can be easily converted to an abstract class. Only a subset of the methods become abstract, because a class is abstracted to
, it does not force us to turn all of its methods into abstractions at the same time. Here's what it looks like:
//: Music4.java//Abstract Classes and methodsImportJava.util.*;Abstract classInstrument4 {intI//Storage allocated for each Public Abstract voidplay (); PublicString What () {return"Instrument4";} Public Abstract voidadjust ();}classWind4extendsInstrument4 { Public voidPlay () {System.out.println ("Wind4.play ()");} PublicString what () {return"Wind4"; } Public voidadjust () {}}classPercussion4extendsInstrument4 { Public voidPlay () {System.out.println ("Percussion4.play ()");} PublicString what () {return"Percussion4"; }171 Public voidadjust () {}}classStringed4extendsInstrument4 { Public voidPlay () {System.out.println ("Stringed4.play ()");} PublicString what () {return"Stringed4"; } Public voidadjust () {}}classBrass4extendsWind4 { Public voidPlay () {System.out.println ("Brass4.play ()");} Public voidAdjust () {System.out.println ("Brass4.adjust ()");}}classWoodwind4extendsWind4 { Public voidPlay () {System.out.println ("Woodwind4.play ()");} PublicString what () {return"Woodwind4"; }} Public classMusic4 {//doesn ' t care on type, so new types//added to the system still:Static voidtune (Instrument4 i) {// ...i.play ();}Static voidTuneall (instrument4[] e) { for(inti = 0; i < e.length; i++) Tune (E[i]);} Public Static voidMain (string[] args) {instrument4[] Orchestra=NewInstrument4[5];inti = 0;//upcasting During addition to the array:orchestra[i++] =NewWind4 (); Orchestra[i++] =NewPercussion4 (); Orchestra[i++] =NewStringed4 (); Orchestra[i++] =NewBrass4 (); Orchestra[i++] =NewWoodwind4 (); Tuneall (orchestra);}} ///:~It can be seen that, in addition to the underlying classes, there is no real change.
Abstract classes and methods