Class Declaration Syntax


As we noted earlier, a class is simply a named block of code. It's a little more complicated than that because a class declaration can include a number of modifiers and keywords. The general syntax of a class declaration is as follows ”

 [modifiers] class <class_name> [extends superclass]               [implements interface1, interface2, ...] {    //  field declarations    //  constructor declarations    //  method declarations    //  other declarations } 

The only required parts of a class declaration statement are the class keyword and a class name. The naming convention for a class is that the first letter is capitalized. Other standard naming restrictions apply. You cannot use a Java keyword as the name of a class. You cannot define two classes with the same name in the same package. A class declaration can also optionally include modifying keywords, information about inheritance, and a list of interfaces the class implements. The modifying keywords define the access and other special characteristics of the class. The body of the class is enclosed with braces and consists of field, constructor, method, and other declarations.

One of the powerful features of object-oriented programming is that you can define a class that extends the capabilities of another class. This concept is called inheritance and is described in detail later in this chapter. A class can also implement one or more interfaces. An interface is a blueprint that defines the methods a class must implement. More details on interfaces can be found in Chapter 10.

Collections of classes (and interfaces for that matter) can be stored in packages. Packages introduce certain access restrictions on the classes they contain. See Chapter 11 for more details on packages.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net