Through my time as a student I have found that using an analogy of something that I already understand to be extremely helpful when remembering or solidifying concepts.
Object Oriented Programming involves the use of classes. When first learning about classes, it seems that the most used analogy includes the car. Throughout this article we will be using a different analogy, the analogy of an oven.
The oven as a class
- Properties of an Oven
- Color
- Dimensions
- Amount of burners
- Methods of an Oven
- heating()
- heatSensing()
Properties and methods are involved in the class blueprint, or in this case, the oven. The values of the properties can change from product to product, and the methods listed here seem to be required by all ovens.
The major concepts
When remembering the major concepts involved with object oriented programming this is the (kind of) mnemonic I use: Easy as Pie
-
Encapsulation: Groups related variables and functions that operate on the object.
- This is how we know what the object is. If the oven in question was able to cool our food would it still be an oven?
-
Abstraction: Hides complexity.
- The main purpose of an oven is to heat food, but how does it do it? Like in the method heating() seen above it is abstracted away. The user doesn't need to know what is going on in the backend and honestly as long as their food is being heated they generally don't care.
-
Inheritance: Eliminates redundant code through creating classes with features used from the parent class.
- A toaster oven IS-A form of oven. Oven would be the toaster oven's parent class, and the toaster oven has the properties and methods inherited from oven.
-
Polymorphism: Many forms of methods.
- Using the toaster oven example, it has the inherited method heating() from oven. The way the toaster oven heats the food is not the same as an oven. Therefore, the toaster oven heating() overrides its parent method of heating().
Top comments (0)