OK, true confession; this is where I usually go into brain melt time when I’ve tried to wrap my head around object oriented programming challenges and assignments. This is the spot where I let the Resistance take full control and say “dude, you’re a tester, you *really* don’t need to get this in depth… ah, but alas, I can’t say that anymore, because I made you all a promise that I wouldn’t.
What is the difference between a Class and an Object? In the Abstract, nothing. They are the same thing at different points in time. Zed uses the following example:
What is the difference between a Fish and a Salmon?
My example I used in a previous exercise would be an equivalent: What is the difference between a Car and a Lamborghini?
Fish and Cars are big order items. We know a fish and a car when we see them. However, there are small characteristics that define where in the Fish world Salmon reside (they aren’t Sea Bass or barracuda), and where in the Car world a Lamborghini fits (they aren’t a Ford or a Chrysler) For that matter, we could consider a Chinook Salmon or a Diablo Lamborghini even more specific, as they are a sub class of their respective class, which is a sub class of a larger class. Mind bendy huh (and strangely, I feel pretty good with that explanation 🙂 ).
Zed takes it a step further. Let’s say that you have caught three Chinook salmon, and named them Frank, Joe, and Mary. What is the difference between Mary and a Salmon?
The answer is “nothing, really” except that, instead of being an abstract Chinook salmon, each of these specific Chinook salmon represent real and tangible items within their class. We would say that these three fish are “instances” of their class that we can call on and use. Instances are also called Objects.
Thus the Chinook Salmon named Mary is an Object. she’s a simple instance of a class Chinook, which is part of the Class Salmon, which is part of the Class Fish. Object, Instance or Class, they’re pretty much the same thing, just more specific as you go down the order.
Still, there are differences. When is a Class a Class and when is an Object an Object in its specific state? There are two phrases that can help us make this determination.
The first is “is-a” and the second is “has-a”. we use “is-a” when talking about objects and classes being related to each other (Chinook is-a Salmon is-a Fish). We use “has-a” when classes are related but merely reference each other (fish has-a set of gills and fins, Lamborghini has-a set of tires and seats).
Below are several examples, and each has a comment labeled ‘##??’ replace each section to reflect if it is an ‘is-a’ or a ‘has-a’ relationship, and what that relationship is.
There is a number of ways that this can be applied and keeping track of it can be a little confusing, but in away it makes a lot of sense when we just think about it on an individual level and “talk it out” like we see here. The relationships can happen on several levels, and certain objects inherit attributes from parent classes.