I don't know that I am suitable or not to explain what is OOP, I really can't measure it. But I encourage myself to bring out this topic because this isn't be far from sharing idea
With my current knowledge (DUMMY), sometime I was confused by OOP too and I don't know why. So I also encourage you who are reading this (yes.. you!!) can share some about OOP too at here, then we can learn it together ![]()
When they were talking about OOP, I just kept silence to listen it and compared to what I know Everyone has their own analogies about OOP concept but I believe their core purposes are still the same if they really can explain what is OOP actually ![]()
If people are talking about OOP, they will bring out at least class and object, inheritance, abstraction, encapsulation and polymorphism.
I believe the comprehension of what I stated above will let you understand the core of OOP although there are some else beside them.
So what is OOP? A programming concept which uses objects and their relationships/interactions to build computer programs/software.
Before we dive into deeper about OOP, we should understand what is a class and object.
Class can be described as a blueprint and also a template that contains same general characteristics and capabilities which can be used by any object.
Forming a class is alike with art of classification, we should determine the general attributes/properties and behaviors/methods inside a class.
eg. class Cat would consist a number of characteristics and behaviors that can be shared by all cats.
Fur color is one of its characteristics and Meow is one of its behaviors. All cats have their own fur color and meow's sound.
If we are talking about object, we should understand what is entity, can be physic or conceptual.
eg. student, car and bicycle are grouped in physic entity whereas bank account can be described as a conceptual entity.
Object is an instance of a class which has its own characteristics and behaviors, a particular version.
eg. YourCat is an instance of class Cat, so YourCat is an object. It has brown fur and can meow 10 times in a minute (capability).
We can have another object which called SomebodyCat, it has black fur and can meow only 8 times in a minute.
From here, I hope that you can understand what is an object.
Next is inheritance, it is a specified class relationship. I ever read a book, the author said that inheritance is '.. is a ..' relationship.
Inheritance is a mechanism that allows a new class can be derived from an existing class.
eg. String Musician is a base class whereas Guitar Player and Violin Player are derived class (child class). So we can say Guitar Player is a String Musician and Violin Player is a String Musician too.
See, '.. is a ..' statement is valid!
See below image.
Abstraction refers to the act of representing essential operations or features without including the implementation details or explanations.
Abstraction is simplifying complex reality by modelling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
Mostly, abstraction related to abstract class and interface.
What is an abstract class? A class exists solely to be derived from, we can't create any instance of this class because it is abstract.
And then what is an interface? Interface isn't a class, it is more 'abstract' than abstract class. Why?
It contains only operations, not implementation whereas abstract class can contain implementation details. It doesn't make sense too if we create any instance of this interface.
This is not an easy task if we want to classify abstract class and interface during development.
See below images.
Encapsulation refers to a mechanism to bound your data and methods, create a boundary so we can control the accessibility of the entity members.
Encapsulation also combines data and functions into single entity. Why we must use encapsulation? To control use and minimize the impact of change in future, those are the key answers.
eg. we have a class Car, this class has a method which has been encapsulated. That method's operation is describing how the machine works internally. In the other hand, is it important for user?
User only want to know how to drive the car, no matter how the machine works, they no need to know the implementations inside that method.
Perhaps they only know 'Drive' the car, so with this point, user doesn't have any control toward that method which is a private method.
'Drive' at here is a method that can be recognized by user or an object which is a public method instead.
Let's say, vendor want to change the implementation of how the machine works in future, do it impact user's activity? I don't think so.
We have already encapsulated the method which contains that implementation, so it looks alike transparent to user.
User won't know and don't want to know how huge or small the change, they only want to know how to drive a car, this is the important key.
See below image for another example.
And the last is polymorphism, it can be described as 'many forms or many shapes' which means an operation may exhibit different behaviors in different instances.
eg. string Musician (base class) has an operation called "TuneYourInstrument". It has 2 derived class currently (Guitar and Violin Player), each derived class overrides operation "TuneYourInstrument" from its base class. Operation "TuneYourInstrument" of each derived class can have different implementation or behavior in this case, Guitar Player plays guitar and Violin Player plays violin. This is called overriding polymorphism.
Assumed that all the musicians exhibit their show in a concert which is leaded by a conductor. There are around 50 types musician with their instrument eg. guitar, violin, harp, ukulele, cello, etc. When the show begins, conductor has to go to each musician for instructing them to play their own instrument if we don't apply polymorphism concept.
"First and second musician, TuneYourGuitar!", "Third, fourth and fifth musician, TuneYourViolin!", and so on... If we apply the polymorphism concept, the conductor will only scream or give an instruction to all musicians in front of them, "All, TuneYourInstrument!" And every musician will tune their own instrument.
See below image.
I hope above explanations can bring us to a clear destination of OOP, no matter how, I am still learning to practice OOP during development.
Happy programming ![]()
Note: idea of some examples I got from a C# ebook (MSDN)
0 comments:
Subscribe to:
Post Comments (Atom)

Post a Comment