|
To compare these two powerful
languages, we must first make a decision: what
should we look at first? Obviously, the syntax
of every programming language is completely
different. However, because these two languages
implement different methodologies.
The argument can be made that
most modern programming languages can be
separated into several major groups. These range
from object-oriented to what will be called
"object-based" to standard procedural languages.
Figure 6.1 shows some popular languages and
which groups they roughly conform to.
Figure 6.1 :
Scale of modern programming
languages.
Because syntax (where to put
semicolons, and so on) is a relatively minor
issue, the initial emphasis will be placed on
understanding exactly what the term
"object-oriented" means.
Object-oriented languages
enable the programmer to create programming
objects that can be reused within a single
application or across multiple applications.
Java refers to these objects as a class.
Classes are user-defined data types that contain
both data and methods. Listing 6.1 shows a
sample pseudo-class named
Auto.
Listing 6.1. A sample
Java class named
Auto.
class
Auto
{
int Number_Of_Doors;
int Number_Of_Seats;
boolean Radio;
int Year;
String Model;
float GetGasMileage();
void Accelerate();
void Stop();
}
As you can see, this class
Auto contains
data (Number_Of_Doors,
Number_Of_Seats,
Radio, and
Year) as
well as methods (GetGasMileage,
Accelerate,
and Stop).
Some people like to think of these as "nouns"
and "verbs." Visual Basic enables the programmer
to create objects that mix data and member
functions. For example, by building a form with
a text field and a button on it, the programmer
has just built an object. This object can be
referenced by other objects and reused across
multiple applications. This is why Visual Basic
is said to be object-based and not procedural.
So, what does it take to qualify as
object-oriented?
For a language to be
object-oriented, it is generally accepted that
it should have several primary features:
-
Encapsulation
- Inheritance
-
Polymorphism
Encapsulation is the process
of encapsulating data and operations within a
manageable interface. The class
Auto meets this
description fully. If a programmer wanted to
instantiate (create) a new
Auto for use in
his program, he would simply add a member
variable of type Auto.
Object-oriented languages also allow the class
creator to limit access to data and operations
within the class. Some data and operations
within each class are considered private
or "for internal use only." This allows the
interface of that class to be controlled for
outside use.
Inheritance is the process of
deriving new objects from existing objects. This
allows the class developer to reuse code that
has already been created for the parent (or
base) class. Here object-based and
object-oriented languages begin to differ. In
Visual Basic, for example, once an "object"
within the program is created, the programmer is
severely limited if he would like to inherit
from it. However, in Java we can extend classes
as much as necessary. Unlike C++, however, Java
will only allow a class to have one parent. This
is known as "single inheritance." As an example,
the class Truck
shown in Listing 6.2 demonstrates inheritance in
Java.
Listing 6.2. The
Truck
class inherits from
Auto.
class
Truck extends Auto
{
boolean Four_Wheel_Drive;
Color color;
}
This code means that class
Truck was
designed to be a "child" of class
Auto.
Therefore, it will inherit all of the public
data and methods that exist in the
Auto class.
Polymorphism is defined as
"the occurrence of different forms, stages, or
types in individual organisms or in organisms of
the same species." This essentially means that
although the Truck
class now inherited all of the data and methods
from Auto,
it is free to reimplement them as it chooses.
You could extend Truck
as shown in Listing 6.3.
Listing 6.3. Adding to
the
Truck
class.
class
Truck extends Auto
{
boolean Four_Wheel_Drive;
Color color;
void Accelerate();
}
The purpose of the
Accelerate
function in the Truck
class might be different (for instance, the
acceleration speed might be affected if
Four_Wheel_Drive
is on). Therefore, any time that any other class
or method calls the
Truck.Accelerate method, the
Truck's
Accelerate
method will be called, not
Auto's. (This
is true even if class
Auto's method
GetGasMileage()
called Accelerate().)
|