Super classes, sub classes, inheritance, oh my!
A class can be considered as something like a blueprint or a template. A class contains both attributes and behaviors, providing something like a definition for objects. In a class, there are properties, which provide the attributes for an object. Let's say you're looking to get a new smartphone. One of the first things you might do is look up the specifications of a phone. Here's a sample of some specifications you might see:
Processor
Operating system
Display
Storage
RAM
Rear camera
Front camera
Battery
Size
Weight
Network
Connectivity
Now, these are all properties of a phone that can change, depending on what brand and model you might get. Different phones have different processors, storage space, battery size, and so on. They all generally have these specifications though, or properties. Now what kind of behaviors would a phone have?
Call
Text
Listen to music
Check email
Browse the internet
Use apps
The behaviors (aka methods) of a phone are pretty vast! Now, a lot of phones can do the same exact things, and have the same exact properties. These behaviors and properties may have small differences, but they still generally follow the same template - in our context, this would be a class! In fact, once you start breaking down a phone into different models, you start dealing with super-classes, sub-classes, and inheritance.
For example, our "luxury" phone might have the fastest processor out there, a large amount of storage, long battery life, and high resolution screen. The "budget" phone though, would have a slower processor, little storage, and so on. The luxury phone might also have some added features, such as a front camera, and mini HDMI port. With behaviors, the luxury model might be able to do more than the budget one. With the front camera and mini HDMI port, there would be new behaviors with those phone. So with this, we'll have two child classes (luxury model and budget model) that share properties and behaviors inherited from the "Phone" super-class, but the value of those properties might be different, and they may add new properties and behaviors!
Since the budget phone and luxury phone share so many things in common, the design process must be pretty similar right? They may even use the same parts between the two phones. To cut down on cost, a phone company might make a template for their phones, and then add bits and pieces depending on the model. In the world of programming, we do something similar by creating classes. In the case of a phone, the company would create a phone using the "luxury" template, or instantiate an object of the phone blueprint! The object in this example would be the physical object that you hold in your hand, the phone.
In my hand, I'm holding an LG G4. This particular phone is an instantiation of the LG G4 blueprint, an object of the LG G4 class. The LG G4 has some cousins with this generation called the V10 and G Flex 2. They have a lot of things in common, but they're still a class of their own. Since they share a lot in common, they may be inheriting properties and behaviors from a class above them, let's say "LG 4th Generation."
Comments
Post a Comment