A class is a template or prototype for each of many object instances made to the class design. The class specifies the properties (data) and methods (actions) that objects can work with.
The second optional group indicates the capability of a class to be inherited or extended by other classes. abstract classes must be extended and final classes can never be extended by inheritance. The default (ie. omitted) indicates that the class may or may not be extended at the programmers discretion.
Class_name has initial letter capitalized by Java convention.
The third option of extends is described in the tutorial on inheritance.
Note 2: Class names begin with a capital. Use lowercase for all other names.
Note 3: It is good programming practice to write separate files for the class templates and the driver or main user program. This allows separate compilation as well as class reuse by other driver programs. A class file can contain more than one associated class but normally its filename is that of the first defined file. A driver program is named the same as the class that contains the main(). Its file may contain other classes as well.
Field Values or Properties
Methods
Types of methods
Syntax of Class
The syntax for a class is:["public"] ["abstract"|"final"]"class" Class_nameThe first optional group indicates the visibility or scope of accessibility from other objects. public means visible everywhere. The default (ie. omitted) is package (aka friendly) or visible within the current package only.
["extends" object_name] ["implements" interface_name]
"{"
// properties declarations
// behavior declarations
"}"
The second optional group indicates the capability of a class to be inherited or extended by other classes. abstract classes must be extended and final classes can never be extended by inheritance. The default (ie. omitted) indicates that the class may or may not be extended at the programmers discretion.
Class_name has initial letter capitalized by Java convention.
The third option of extends is described in the tutorial on inheritance.
The fourth option of implements is described in the tutorial on interfaces.
A simple example of a class specification is a box. The box has length, width and height properties as well as methods for setting dimensions and displaying its volume.public class BoxNote 1: There is no main method in a class defining template!
{
// what are the properties or fields
private int length, width, height;
// what are the actions or methods
public void setLength(int p) {length=p;}
public void setWidth(int p) {width=p;}
public void setHeight(int p) {height=p;}
public void showVolume() {System.out.println(length*width*height);}
}
Note 2: Class names begin with a capital. Use lowercase for all other names.
Note 3: It is good programming practice to write separate files for the class templates and the driver or main user program. This allows separate compilation as well as class reuse by other driver programs. A class file can contain more than one associated class but normally its filename is that of the first defined file. A driver program is named the same as the class that contains the main(). Its file may contain other classes as well.
Field Values or Properties
Methods
Types of methods
No comments:
Post a Comment