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, a study of syntax will be delayed until later on in the chapter.
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.
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 Autocontains 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
Encapsulation is the process of encapsulating data and operations within a manageable interface. The class Automeets 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
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 Truckshown in Listing 6.2 demonstrates inheritance in Java.
Listing 6.2. The Truckclass inherits from Auto.
class Truck extends Auto{boolean Four_Wheel_Drive;Color color;}
This code means that class Truckwas 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
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 Truckclass 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 Truckclass.
class Truck extends Auto{boolean Four_Wheel_Drive;Color color;void Accelerate();}
The purpose of the Acceleratefunction in the Truck class might be different (for instance, the acceleration speed might be affected if Four_Wheel_Driveis on). Therefore, any time that any other class or method calls the Truck.Accelerate method, the Truck's Acceleratemethod will be called, not Auto's. (This is true even if class Auto's method GetGasMileage() called Accelerate().)
No comments:
Post a Comment