What’s So Good about Object-Oriented Java?
Java is a way of programming using object-oriented programming. As an example, imagine that you’ve already written a computer program to keep track of the building instructions for houses in a new development. Then, the big boss decides on a modified plan — a plan in which half the houses have three bedrooms and the other half have four.
If you use the old FORTRAN/C style of computer programming, your instructions look like this:
Dig a ditch for the basement. Lay concrete around the sides of the ditch. Put two-by-fours along the sides for the basement’s frame. ...
This would be like an architect creating a long list of instructions instead of a blueprint. To modify the plan, you have to sort through the list to find the instructions for building bedrooms. To make things worse, the instructions could be scattered among pages 234, 394–410, 739, 10, and 2. If the builder had to decipher other peoples’ complicated instructions, the task would be ten times harder.
Starting with a class, however, is like starting with a blueprint. If you decide to have both three- and four-bedroom houses, you can start with a blueprint called the house blueprint that has a ground floor and a second floor, but has no indoor walls drawn on the second floor.
Then you make two more second-floor blueprints — one for the three-bedroom house and another for the four-bedroom house. (You name these new blueprints the three-bedroom house blueprint and the four-bedroom house blueprint.)
Your builder colleagues are amazed with your sense of logic and organization, but they have concerns. They pose a question. “You called one of the blueprints the ‘three-bedroom house’ blueprint. How can you do this if it’s a blueprint for a second floor and not for a whole house?”
You smile knowingly and answer, “The three-bedroom house blueprint can say, ‘For info about the lower floors, see the original house blueprint.’ That way, the three-bedroom house blueprint describes a whole house. The four-bedroom house blueprint can say the same thing. With this setup, you can take advantage of all the work you already did to create the original house blueprint and save lots of money.”
In the language of object-oriented programming, the three- and four-bedroom house classes are inheriting the features of the original house class. You can also say that the three- and four-bedroom house classes are extending the original house class.
The original house class is called the superclass of the three- and four-bedroom house classes. In that vein, the three- and four-bedroom house classes are subclasses of the original house class. Put another way, the original house class is called the parent class of three- and four-bedroom house classes. The three- and four-bedroom house classes are child classes of the original house class.
Needless to say, your homebuilder colleagues are jealous. A crowd of homebuilders is mobbing around you to hear about your great ideas.
So, at that moment, you drop one more bombshell: “By creating a class with subclasses, we can reuse the blueprint in the future. If someone comes along and wants a five-bedroom house, we can extend our original house blueprint by making a five-bedroom house blueprint. We’ll never have to spend money for an original house blueprint again.”
“But,” says a colleague in the back row, “what happens if someone wants a different first-floor design? Do we trash the original house blueprint or start scribbling all over the original blueprint? That’ll cost big bucks, won’t it?”
In a confident tone, you reply, “We don’t have to mess with the original house blueprint. If someone wants a Jacuzzi in his living room, we can make a new, small blueprint describing only the new living room and call this the Jacuzzi-in-living-room house blueprint. Then, this new blueprint can refer to the original house blueprint for info on the rest of the house (the part that’s not in the living room).”
In the language of object-oriented programming, the Jacuzzi-in-living-room house blueprint still extends the original house blueprint. The Jacuzzi blueprint is still a subclass of the original house blueprint. In fact, all the terminology about superclass, parent class, and child class still applies. The only thing that’s new is that the Jacuzzi blueprint overrides the living room features in the original house blueprint.
In the days before object-oriented languages, the programming world experienced a crisis in software development. Programmers wrote code, then discovered new needs, and then had to trash their code and start from scratch.
This problem happened over and over again because the code that the programmers were writing couldn’t be reused. Object-oriented programming changed all this for the better (and, as Burd said, Java is “A Great Object-Oriented Language”).