Valentine’s Special Cont.
White Day (is already over) in Japan! Hurrah to the power of marketing!
This is a continuation of the Valentine’s special, in ENGLISH! 😉 About Object-oriented programming (OOP)!
First, what is an object? Objects are instance of class, which contains behavior (methods) and data (members or attributes).
Object-oriented programming 4 principles
encapsulation
abstraction
inheritance
polymorphism
What…?
As a self taught programmer, I have read the explanation more than 10 times and am still confused, so I want to try to reimagine it with real life examples and hopefully that sticks. Since I wrote about cookies, the sweet delicious kind, I’m going to try to explain it in terms of that for each of the principles.
OOP
encapsulation
Prevents direct access to private state and only allows access to public elements
Examples of private state of a cookie:
sweetness
hardness
Since you can’t physically affect the sweetness or hardness of a real life cookie parameters unless you live in the digital world Matrix style, you could do something about it with a real physical method such as:
sprinkleSugar()
dipInChocolate()
dunkInMilk()
But why do we need encapsolution? To hide information from misuse and limit interdependency and reduce system complexity.
abstraction
Think of abstraction as a black box, don’t have to know what’s going on. It is about intent of the class rather than the actual implementation.
Most people don’t know the recipe for a cookie, its nutritional composition, nor its caloric content when eating them. All they know that it is sweet and delicious, and fills their tummy.
Also cookies go through a huge process before becoming part of your groceries. Although people could look into the “source code”, generally we don’t look into where the cookie is made, which company, or what machine made them, where the ingredients come from, or how it transported to the store where it was bought etc. When we eat a cookie, we don’t need to know that much about it, so abstraction!
Another example is when baking a cookie, you need to set a certain temperature, and timer for the oven, you don't need to know how the oven works to complete the task of baking.
Abstraction could also be stripped to (or rather composed of) its very basic interfaces, such as shapes, flavor, and toppings, depending on your programming needs.
shape interface
square
oval
bar
flavor interface
butter
chocolate
ginger
topping interface
chocolate
nuts
inheritance
There are many kinds of cookies in the world, we think of them as similar but they are not entirely the same. For example we know the base cookie and chocolate cookie are similar in the sense that the difference is the cocoa powder. They would still share the common method such as calculateCalories() where you would add up the calories per gram of each ingredient. This would make chocolate cookie the child class of cookie class since it could inherit the calulateCalories() method. Then of course, a cookie is a sweets, and they can be accessed when wanting a snack. And sweets are part of the food class which can share a common method to fill up our belly, and it is ultimately where the calulateCalories() method could be inherited from in a program.
polymorphism
When baking chocolate cookie, you could use all the same methods (recipe) you used when you baked the regular cookie but you would need to add cocoa when mixing the batter of the chocolate version. They share common interface, but the chocolate version would have slightly different method when mixing the batter.
Common interface with sweets such as cake and biscuits, in the sense the process and the ingredients are very similar and when writing a program you could write it in such a way.
THE END
Thank you for reading!
Hopefully now you have a better image of what the principles of OOP are :)!
Cookies were not a good example in retrospect but keeping in with the theme here. 😉
BONUS
overloading
class CalculateCalories{
static int add(int a, int b) {return a+b}
static int add(int a, int b, int c) {return a+b+c}
static int add(int a, int b, int c, int d) {return a+b+c+d}
}
overriding
class Cookie {
void identifyMe() {System.out.println(“I am a cookie”)}
}
class ChocolateCookie extends Cookie {
void identifyMe() {System.out.println(“I am a chocolate cookie”)}
}