I understand when you are studying basic oop concepts; bigger and complex looking words like Abstraction or Encapsulation can blow up your mind. When already Abstract Classes are messing with your coding knowledge, this encapsulation thing gives headache. Well, here I am to explain you in simple word what is encapsulation in OOP.
What is Abstraction?
Almost everyone of you have driven a car once in a while. Have you really thought when I put a break on what happens inside a car that makes it reduce the speed and stop suddenly? Have you thought how that machinery inside the car works? No! Because you have just cared about putting break on and reduce the speed. This is abstraction!
Abstraction is hiding irrelevant details from viewer's perspective. In case of example of car, you are a user and automobile engineer have taken enough efforts to hide details inside the car and just give you one part to hit on when you need to reduce the speed.
Now how to achieve abstraction in object oriented languages? Abstract methods are the methods which have only declaration but no definition. In other words, abstract methods are needed to be overridden by child class in order to use them. A class having such methods is called as Abstract Class. You may refer my previous post to understand when to use an abstract class.
To understand what is abstraction you need to understand real life examples about it and do some coding in your favorite object oriented language. And if you still don't get it, feel free to comment below and I will help you out!
"Just use keyword abstract."
ReplyDeleteBefore you try teaching people about what abstraction is, get it straight yourself first. An abstract class/method is no more or less abstraction than a concrete class/method. Abstration can be achieved by non-abstract classes/methods just as well. If you have a method "Brake()" (using your car metaphor here) you have no idea what it does internally (and you shouldn't have, nor care) to achieve the desired effect (i.e. stopping the car).
The abstract keyword simply signifies that that particular class does NOT have an actual implementation for that method and that derived classes MUST implement it (or forward it to other derived classes).
I'm very sorry but this "article" does more harm than good; I'd advise to take it offline for a while until you have corrected it.
Thank you for clearing these things. I messed up while writing it. I think I should better delete that sentence and explain there that abstract class is an incomplete class and so is abstract method. Thank you once again.
DeleteIt's not that one sentence that was wrong, it's the entire premise of this post that is incorrect. Abstraction (hiding things) in the sense you mean has nothing to do with abstract methods/classes.
DeleteAs I explained: A method Brake() can perfectly be non-abstract and still "abstract away" the internal details that in the end cause the car to slow down / stop. I am very sorry I'm not able to explain this more clearly; first because I'd need to go a whole lot deeper, second because my native language is Dutch and third because I'm not a teacher. But the entire concept and premise of this post is wrong the second you associate abstraction with the abstract keyword. Abstraction is achieved by hiding details ("black box"), not by using the abstract keyword. My second comment (6:30 AM) has some links in it that may explain better.
You come across (no flame intended!) as being a student learning these concepts yourself and along the way blogging about it. If that's the case I'd like to advise you to take another look at your book or consult your teacher/professor. Either way; the direction you're heading in is a "screwed up vision of OOP" so to say.
DeleteTo be clear on my last comment: The abstraction is the Brake() method; you don't need to know which cylinders it controls to apply pressure to which brake pads. Actually, you don't care about cylinders nor brake pads at all; all you care about is that it slows down / stops the vehicle. How this is achieved is none of your concern unless you are the one implementing the method. Going one step further; using SOLID principles (Liskov substitution principle), you should be able to swap out the class implementing the Brake() method with another one that doesn't control cylinders / brake pads but which might throw out an anchor to achieve the "stopping effect" or open the trunk for more windpressure or switch the gears into "reverse" or ... you get the idea. There's your abstraction. It has nothing to do with the abstract keyword.
ReplyDelete