Object concepts in Java A Java program is a software system -- i.e. an executing Java program is a network of software objects. The writing of a program in Java consists of defining a set of classes using the Java programming language. Simple Java programs may consist of a single class, or just two or three classes, while complex programs can consist of many tens, hundreds or even thousands of classes. Usually (preferably) the writing of Java programs happens after the desired system has been modelled and represented using object concepts. Such a model acts as a specification of the system, and as a design from which a computer program can be implemented. The model of a class involves defining the attributes and operations of a class. When implementing a class in Java: Java provides three different types of variable, an overview of which are given in this unit. A more detailed investigation of Java types and variables is conducted in the next unit. The implementation of a class in Java A class is implemented by writing Java program statements in a file with the same name as the class. So, for example, a class called "BankAccount" would be implemented in a file called "BankAccount.java". The contents of such a class would be something like: file: BankAccount.java class BankAccount { <variable1 defined here> <variable2 defined here> <variableN defined here> <method1 defined here> <method2 defined here> <methodN defined here> } Notice how a class is defined using the Java reserved word "class" followed by the name of the class. Also notice how the use of the open "{" and close "}" braces are used to encapsulate the contents of the class, i.e. all variables and methods are written inside these braces to define the class. The implementation of class attributes in Java Attributes are implemented as Java variables. Consider the following attributes from a model of a class of BankAccount objects: To implement these attributes in Java we need to decide what type (kind) of Java variable to use for the storing of each of these attributes. The term "type" has a particular meaning in computer programming, and in fact many object concepts have evolved from earlier work on a form of programming involving abstract data types. In a computer program any variable is of a specified type. Each computer programming language provides facilities for its own particular set of variable types. Pure object-oriented programming languages provide only one type of variable -- the object. Attributes store different types of value. Examples of each of the attributes for our BankAccount class might be: accountNumber: 00101234, 02224488, 01012001 accountHolderName "James Smith", "Leopold Schubert", "Pierre Dupont" accountHolderDateOfBirth 06-05-1945, 11-11-1990, 28-12-1975 balance 1002.56, 0.01, -256.99 overdraftLimit -50, -500, -875
The programmer can infer from examples such as these the types of value for each attribute: One possible implementation of the above attributes as Java variables is as follows: file: BankAccount.java class BankAccount { // variables char accountNumber[]; String accountHolderName; Date accountHolderDateOfBirth; float balance; int overdraftLimit; // methods <method1 defined here> <method2 defined here> <methodN defined here> } Java is not a pure object-oriented programming language. One way in which Java is not pure is that not every attribute is implemented as a object variable. Java provides three types of variable: All three types of variable are introduced in the following units. In the example above we can see that to implement the variables of a class in Java, as well as stating the variable name, we also need to precede this with the name of the type of variable it is. Above we see examples of five different types: Although the details are not important for this illustrative example, the first is an array of characters, the second and third are objects of classes String and Date, the fourth is a floating point (decimal) number, and the last an integer (whole) number. Java Programming convention: Classes are written with a capitalised first letter (String, Date) Java Programming requirement: Primitive types must be written with a lower case first letter (char, float, int), since this is how they have been named in the definition of the Java programming language Back to top 
RITSEC - Global Campus Copyright ?1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg |