Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  


AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Abstract classes and methods

java

+ Font mai mare | - Font mai mic



DOCUMENTE SIMILARE

Trimite pe Messenger
return
Making a button
Capturing an event
Calculation details
Overriding vs
private: you canít touch that
Applet restrictions
Utilities: Making a Collection or Map unmodifiable
RTTI considered harmful
Choice, Menus, And Checkboxes


Abstract classes
and methods

In all the instrument examples, the methods in the base class Instrument were always ďdummyĒ methods. If these methods are ever called, youíve done something wrong. Thatís because the intent of Instrument is to create a common interface for all the classes derived from it.




The only reason to establish this common interface is so it can be expressed differently for each different subtype. It establishes a basic form, so you can say whatís in common with all the derived classes. Another way of saying this is to call Instrument an abstract base class (or simply an abstract class). You create an abstract class when you want to manipulate a set of classes through this common interface. All derived-class methods that match the signature of the base-class declaration will be called using the dynamic binding mechanism. (However, as seen in the last section, if the methodís name is the same as the base class but the arguments are different, youíve got overloading, which probably isnít what you want.)

If you have an abstract class like Instrument, objects of that class almost always have no meaning. That is, Instrument is meant to express only the interface, and not a particular implementation, so creating an Instrument object makes no sense, and youíll probably want to prevent the user from doing it. This can be accomplished by making all the methods in Instrument print error messages, but this delays the information until run-time and requires reliable exhaustive testing on the userís part. Itís always better to catch problems at compile time.

Java provides a mechanism for doing this called the abstract method. This is a method that is incomplete; it has only a declaration and no method body. Here is the syntax for an abstract method declaration:

abstract void X();

A class containing abstract methods is called an abstract class. If a class contains one or more abstract methods, the class must be qualified as abstract. (Otherwise, the compiler gives you an error message.)

If an abstract class is incomplete, what is the compiler supposed to do when someone tries to make an object of that class? It cannot safely create an object of an abstract class, so you get an error message from the compiler. This way the compiler ensures the purity of the abstract class, and you donít need to worry about misusing it.

If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class. If you donít (and you may choose not to), then the derived class is also abstract and the compiler will force you to qualify that class with the abstract keyword.

Itís possible to declare a class as abstract without including any abstract methods. This is useful when youíve got a class in which it doesnít make sense to have any abstract methods, and yet you want to prevent any instances of that class.

The Instrument class can easily be turned into an abstract class. Only some of the methods will be abstract, since making a class abstract doesnít force you to make all the methods abstract. Hereís what it looks like:




Hereís the orchestra example modified to use abstract classes and methods:

//: Music4.java

// Abstract classes and methods

import java.util.*;

abstract class Instrument4

public abstract void adjust();

}

class Wind4 extends Instrument4

public String what()

public void adjust()

}

class Percussion4 extends Instrument4

public String what()

public void adjust()

}

class Stringed4 extends Instrument4

public String what()

public void adjust()

}

class Brass4 extends Wind4

public void adjust()

}

class Woodwind4 extends Wind4

public String what()

}

public class Music4

static void tuneAll(Instrument4[] e)

public static void main(String[] args)

} ///:~

You can see that thereís really no change except in the base class.

Itís helpful to create abstract classes and methods because they make the abstractness of a class explicit and tell both the user and the compiler how it was intended to be used.






Politica de confidentialitate



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 380
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2022 . All rights reserved

Distribuie URL

Adauga cod HTML in site