Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / Database concepts.pdf
Скачиваний:
29
Добавлен:
12.05.2015
Размер:
6.96 Mб
Скачать

Java Overview

Figure 14–7 Using Inheritance to Localize Behavior and State

class Employee

id

last name

Employee class has two subclasses, PartTimeEmployee and FullTimeEmployee, rather than using attributes of Employee to differentiate between different Employee types.

Note: We could have made Employee an interface.

class PartTime Employee

schedule

class FullTime Employee

bonus

PartTimeEmployees have to track their schedules, while Full-TimeEmployees are eligible for bonuses.

class ExemptEmployee

salaryToDate()

class NonExemptEmployee

salaryToDate()

Each FullTimeEmployee is considered "exempt" if he works for a monthly salary, or "non-exempt" if he works at an hourly rate. Each one computes salaryToDate differently.

Instances of Class B are substitutable for instances of Class A, which makes inheritance another powerful construct of object-oriented languages for improving code reuse. You can create new classes that define behavior and state where it makes sense in the hierarchy, yet make use of pre-existing functionality in class libraries.

Interfaces

Java supports only single inheritance; that is, each class has one and only one class from which it inherits. If you must inherit from more than one source, Java provides the equivalent of multiple inheritance, without the complications and confusion that usually accompany it, through interfaces. Interfaces are similar to classes; however, interfaces define method signatures, not implementations. The methods are implemented in classes declared to implement an interface. Multiple inheritance occurs when a single class simultaneously supports many interfaces.

SQL, PL/SQL, and Java 14-35

Java Overview

Polymorphism

Assume in our Employee example that the different types of employees must be able to respond with their compensation to date. Compensation is computed differently for different kinds of employees.

FullTimeEmployees are eligible for a bonus

NonExemptEmployees get overtime pay

In traditional procedural languages, you would write a long switch statement, with the different possible cases defined.

switch: (employee.type) { case: Employee

return employee.salaryToDate; case: FullTimeEmployee

return employee.salaryToDate + employee.bonusToDate

...

If you add a new kind of Employee, you must update your switch statement. If you modify your data structure, you must modify all switch statements that use it.

In an object-oriented language such as Java, you implement a method, compensationToDate(), for each subclass of Employee class that requires any special treatment beyond what is already defined in Employee class. For example, you could implement the compensationToDate() method of

NonExemptEmployee, as follows:

private float compensationToDate() {

return super.compensationToDate() + this.overtimeToDate();

}

You implement FullTimeEmployee’s method, as follows:

private float compensationToDate() {

return super.compensationToDate() + this.bonusToDate();

}

The common usage of the method name compensationToDate() lets you invoke the identical method on different classes and receive different results, without knowing the type of employee you are using. You do not have to write a special method to handle FullTimeEmployees and PartTimeEmployees. Thisability for the different objects to respond to the identical message in different ways is known as polymorphism.

In addition, you could create an entirely new class that does not inherit from

Employee at all—Contractor—and implement a compensationToDate()

14-36 Oracle9i Database Concepts

Java Overview

method in it. A program that calculates total payroll to date would iterate over all people on payroll, regardless of whether they were full-time, part-time, or contractors, and add up the values returned from invoking the compensationToDate() method on each. You can safely make changes to the individual compensationToDate() methods with the knowledge that callers of the methods will work correctly. For example, you can safely add new fields to existing classes.

The Java Virtual Machine (JVM)

As with other high-level computer languages, your Java source compiles to low-level machine instructions. In Java, these instructions are known as bytecodes (because their size is uniformly one byte of storage). Most other languages—such as C—compile to machine-specific instructions—such as instructions specific to an Intel or HP processor. Your Java source compiles to a standard, platform-independent set of bytecodes, which interacts with a Java virtual machine (JVM). The JVM is a separate program that is optimized for the specific platform on which you execute your Java code. Figure 14–8 illustrates how Java can maintain platform independence. Your Java source is compiled into bytecodes, which are platform independent. Each platform has installed a JVM that is specific to its operating system. The Java bytecodes from your source get interpreted through the JVM into appropriate platform dependent actions.

SQL, PL/SQL, and Java 14-37

Java Overview

Figure 14–8 Java Component Structure

Java Applications

Java Virtual Machine

Operating System

When you develop a Java program, you use predefined core class libraries written in the Java language. The Java core class libraries are logically divided into packages that provide commonly-used functionality, such as basic language support (java.lang), I/O (java.io), and network access (java.net). Together, the JVM and core class libraries provide a platform on which Java programmers can develop with the confidence that any hardware and operating system that supports Java will execute their program. This concept is what drives the “write once, run anywhere” idea of Java.

Figure 14–9 illustrates how Oracle’s Java applications sit on top of the Java core class libraries, which in turn sit on top of the JVM. Because Oracle’s Java support system is located within the database, the JVM interacts with the Oracle database libraries, instead of directly with the operating system.

14-38 Oracle9i Database Concepts

Соседние файлы в папке Oracle selected docs