ooConcepts.txt Feb 14 2004 Bikle ------------------ This page contains a summary of Object Oriented Concepts. Object ------ When you see a car driving down the street, think of that as an Object. Class ----- When you go to the autoparts store and you see a manual full of detailed information about your car, think of that manual as a Class. Class is synonymous with blueprint or recipe. A Class is a description of how to build an Object. Inheritance ('is a' Relationship) ---------------------------------- A VW Bug 'is a' car. A Chevy Nova 'is a' car. A Ford Pinto 'is a' car. The above three car types are classes which inherit from the more general Car class. This perspective of inheritance is called specialization. Generalization('is a' Relationship) ----------------------------------- The first time I saw a moped I asked, "What is it?" "It has a motor and pedals." Is it a bike? Is it a motorcycle? I generalized, "It's a two wheeled vehicle." This is a different perspective of inheritance. 'has a' Relationship (Aggregation) ------------------------------------ A car 'has a' motor. This is an example of a 'has a' Relationship. 'has a' Relationship (Composition) ----------------------------------- Forget cars; think of a package-deal-trip to Hawaii. The trip 'has a' rental car reservation. It 'has a' hotel reservation. It 'has a' flight reservation. If you cancel the trip, all of the reservations disappear. This is different than the above car-motor example. If the car gets totaled in a wreck, you might be able to still sell the motor. 'uses' Relationship ------------------- A car uses gas. This is an example of a 'uses' Relationship. You could also say: A car uses a motor. If you want to track WHICH motors a car has worn out, it might be better to use a 'has a' Relationship between car and motor. If you don't care about motors, it might be more appropriate to say, "A car 'uses' a motor". Polymorphism ------------- Forget cars; look at documents. Suppose I have a web page in front of me. I can print it by pulling down a menu from the upper left and then clicking 'Print'. I can do the same for a spread sheet. Polymorphism allows me to print any document whether it is an e-mail, PDF file, or picture of a car. Encapsulation ------------- Suppose I'm driving my car on 101. I steer via the steering wheel. I speed up via the gas pedal. I slow down by pressing the brake. At that moment I use three inputs to control the car. I ignore the mechanics affected by these inputs. I don't care that the gas pedal feeds gas to the motor. Encapsulation allows me to ignore things I don't care about. Collection ---------- Suppose I see a parking lot full of cars. I think of this as a collection of cars. Suppose I see a car dealership with many cars parked on its lot. Is a car dealership a Collection? I say, "No." A parking lot makes a good collection because all it does is collect cars. A dealership does more than that. It might be appropriate to say that a dealership 'has a' parking lot (which is a collection of cars). Packages -------- Classes may be grouped into 'Packages'. A class is like a recipe; it describes how to make something. Suppose I write class named BirthdayCake. Then, you write class named BirthdayCake. Then, we publish these classes on the internet. How can people discern my class from your class? The answer is, I put my class in a 'package'. I would put it in a package named: com.bikle.recipes. Then people would refer to my BirthdayCake class as this: com.bikle.recipes.BirthdayCake