Ads 468x60px

Monday, July 25, 2011

Java as an Object-Oriented Language


 Java's class structure is made up of the following major components: classes, hierarchy, variables, methods, and inheritance.

Classes

The key to understanding Java's object-oriented design is understanding what classes are and what you can do with them. Classes are templates that you use to create actual objects. The instructions contained in a class are used to create one or more objects, which can be called instances of classes in Java. When you create an object from a class, you instantiate the object, which means you create an instance of the class. The words instances and objects are used interchangeably throughout the discussions of classes in this book, depending on the context of the sentence in which they're used.
In object-oriented programming, you can think of an object as you would any real-world object, for example, a rectangle. The actual rectangle would be an instance of the class Rectangle.
A very rudimentary declaration of a class is as follows:
class  classname {
//class instructions
}
The instructions in a class are made of two basic components: variables that hold data, and methods that manipulate the data.
Before you begin creating the classes in your application, you must design the class structure. Java's class structure is organized into a hierarchy.

Hierarchy

Classes are organized into a hierarchy to allow you to easily reuse code. When you write a Java program, first determine which objects you'll need to use in your code. Then determine what variables and methods the object's class must store. When you know what instructions your classes contain, plan your hierarchy.
Without planning your hierarchy first, you would begin creating a class for each object individually and undoubtedly would find that you are repeating instructions from class to class. In a hierarchy, instructions common to groups of objects are separated out in parent classes, or superclasses, and can be used by all of their subclasses. Therefore, when you plan your hierarchy, you would group objects by the instructions that they have in common and organize them into a hierarchy.
Superclasses are used as templates to create subclasses with variables and methods that make each subclass unique. Each superclass can be a parent to one or many other subclasses. Unlike in C++, a subclass can have only one superclass. Therefore, the Java class hierarchy looks something like the one shown in Figure 1.1.
Figure 1.1: Java class hierarchy.
If a superclass is not defined when a class is declared, the class is automatically made a subclass of Java's Object class. Every class in a Java program is a descendant of Object. Object itself has no superclass.

Variables

Classes store information that describe objects in instance variables. When objects are created from a class, they contain new instances of the class' variables. These instance variables can have values that are different from one object to the next. Values of instance variables are called data. When an instance variable's data is changed, it affects only the individual object. There is a way in Java to assign a variable to a class that, if changed, is changed in all instances of the class. Such a variable is called a static variable, which you learn about later in this chapter in the section called "Static Methods and Variables."
The basic declaration statement of a variable is this:
datatype variablename;
Two variables that might be declared for a rectangle are length and width. They could be declared in one line because they have the same data type. (You explore data types in Table 1.1 of this chapter.) Their declaration statement might be this:
int length, width;
If you created an object from a class with only these two variables declared, the object would simply hold this data. It would not know to draw lines with these values to form a rectangle. Methods must be declared to use the data in variables.

Methods

Methods are functions that must be associated with individual classes. In C and C++ and other procedural languages, functions can be placed anywhere in the code. In Java, they must be stored within classes. Instances of methods are created when instances of classes are created. Unlike variables, methods are not duplicated in objects-their code is stored only in the class.
When an object's method is invoked from its class, it uses the data of variables in the object.
Every method returns a value if it is not declared as void. To declare a method, you use the following statement:
returntype methodname (parameter list) {
    //method code
}
At this point in the chapter, you won't get into coding the method that draws the rectangle. After you've learned the basic coding elements of Java later in this chapter, exercises in coding methods will be more useful.

Inheritance

Variables and methods that are stored in classes in the class hierarchy are inherited by subclasses, so you do not need to re-create them. Objects created from a subclass will contain not only the instances of the variables and methods of the subclass, but also its superclass' variables and methods, as well as those of the parent of its superclass, and so on. When a variable or method is referenced in an object, it is retrieved in a specific order: Java first searches for it in the current class, then, if it is not found, it searches the parent class, and so on.
In summary, objects in your Java program are created from classes that contain variables and methods to describe the object. Classes are organized into a hierarchy in which classes inherit functionality from parent classes, which allows for reusable code. These basic concepts of the structure of Java's object-oriented programming language will become clearer to you as you read through the rest of the sections.

Basic Coding Elements

Your Java code is used to create classes, objects, interfaces, and packages. You'll learn how to create each of these in Chapter 4. This section outlines the elements of the code you need to use to create them.
Your code is written in a series of statements, which can be organized into blocks. These statements contain data and operators, which are components of expressions. You can annotate your code using comments, which makes the code more understandable.

Statements

Any line of code before a semicolon is known as a statement and is executed by the Java interpreter when it hits the semicolon; after executing that statement, the interpreter moves to the next statement. Each statement contains instructions for using data and operators.

Expressions

An expression is a part of a statement that uses data and operators to return a value.

Blocks

A block is a collection of statements enclosed in curly braces. Any variables that you declare and that assign values within a block are erased when the flow of execution leaves the block. The block in which the variable's value exists is called the scope of the variable.

Comments

Comments are used to annotate the code so that a reader can understand the purpose of certain lines and blocks of code. Comments are ignored by the Java compiler. Multiline comments are preceded by /* and are ended with */. Single line comments are preceded by //. The double slash "comments out" text only to the end of a line. A comment would appear as follows:
/*  This declares the length variable for the Rectangle class   */
int length;
or
int length;   //Declares the length variable for the Rectangle class

0 comments:

Post a Comment