Chapter 6 - Introduction to Object Oriented Programming
Concept
I have delayed talking about Object Oriented Programming until now so people who were unfamiliar with the concept could begin writing applet quickly without being intimidated by this buzz phrase. You already have seen many words such as class, method, package, and library that have been derived from OOP.
Object Oriented Programming is an attempt to organize and structure computer code and programs into a state that mirrors life. Things are broken up into smaller, simpler objects which make the language more flexible and powerful.
Think of a program as building something. Rather than having an enormous tool that tries to do everything well you have a tool chest that contains many tools. Each tool fulfills a specific role. The screwdriver is for tightening screws, the wrench,tightens bolts, and the saw cuts wood. You grab the tool you need for the job and put it back when it is done. This works much better than a tools trying to be a saw, wrench, and screwdriver all at once.
Another way to look at it is to consider the example of a bicycle. The bicycle is made up of different parts that each serve a purpose. You do not need to know how exactly each piece works. All you need to know is how they fit together and operate together.
Classes and instances
A class is a description or template that serves as an abstract idea of an object. It contains all the features that are similar. For instance, you could have a class of televisions. This television class would describe the features common to all televisions (picture tube, speakers, etc).
You can then create many specific televisions from this abstract description of a television. A specific object created from a class is called an instance of that class. There can be many instances of the television class each with different details (size, weight, color, stereo, etc).
So for each television we wish to create we only need to create an instance of the television class and give it certain attributes and behavior. We do not need to rewrite the television code each time we need a television.
Instance variables and methods
Attributes are specific details in the class that differentiate separate instances of the class. Attributes are stored in variables which are called instance variables and can be initialized at the birth of the instance or during the life of the instance. These attributes also store the state of the given instance and thus can change over time.
The television in your living room would be an instance of the television class. It has a certain weight, dimensions, picture tube size, and cost. These would be stored probably as integers in instance variables (if rounding). Your television also is color or black and white, a trinotron or not, and capable of cable or not. These attributes would probably be stored as boolean instance variables (such as isColor). Your television is also on or off which corresponds to the state your TV is in. This would also be stored as a boolean variable.
int tubeSize; int weight; boolean isOn; boolean isColor; int volume;The behavior of the television is recorded by methods which are usually defined inside the instance. These methods tell the television what to do if something changes or if another object requests an action. Methods are functions written inside classes that then operate on the instance. For example, you may want a method that turns on and off the television, turn up the volume, or turn down the volume.
void turnOnOff() { if (isOn == true) isOn = false; else isOn = true;The turnOnOff() method checks to see if the television is on and if it is it turns it off. If the television was not on it then turns it on. The void just means that this method does not return a value when it is done.
Organization
Java provides a way to organize your programs using inheritance, interfaces, and packages. These three concepts will be touched upon here and then covered in more detail in a later section.
Inheritance allows you some flexibility when writing classes. Your classes can inherit features from other classes and will thus be based on them. You can then add features that make your class different which then creates a certain class hierarchy.
Under Construction