What is the difference between encapsulation and abstraction in oops
What is the word abstract means? Google search says the English word abstraction means. And thats right in case of abstract class too. It is not hiding the content of the method but the method's content is already empty not having a physical or concrete existence but it determines how a method should be existing in thought or as an idea or a method should be in the calss. An abstract method in a class forces the child class to implement that function for sure with the provided signature!
If I am the one who faced the interview, I would say that as the end-user perspective abstraction and encapsulation are fairly same. It is nothing but information hiding. As a Software Developer perspective, Abstraction solves the problems at the design level and Encapsulation solves the problem in implementation level.
And abstraction helps to do push or pop on stack without worrying about what are steps to push or pop. Abstraction As the name suggests abstract means summary or brief about somtehing. In case of OOP Abstract Classes are the ones which do not contain every information about that object in real world, for eg.
So, you get abstracted information about the room which you care about. On the other hand, Encapsulation is basically capsulating the related information together, for eg. Now the switch object has all internal wirings which are required to switch that bulb ON, but you really do not care about those wirings.
You care only about bulb is switched ON or not. Now the switch class has the information but it is hidden to you. On the other hand room class does not have the information about wiring design of a hotel room since it is not even in the context of online booking of the room.
Thus, the abstraction is more related to classes and encapsulation is more related to internal of the class objects, attributes and methods. Abstraction: what are the minimum functions and variables that should be exposed to the outside of our class.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Difference between Encapsulation and Abstraction Ask Question. Asked 8 years, 8 months ago. Active 3 days ago.
Viewed k times. Can anyone please justify it with a simplest example? Improve this question. Raman Sahasi WpfBee WpfBee 2, 6 6 gold badges 22 22 silver badges 28 28 bronze badges.
WPFbee Unfortunate! I don't know why people compare Abstraction and Encapsulation. Why can't we accept them as two different OOP concepts. Possible duplicate of difference between abstraction and encapsulation? Add a comment. Active Oldest Votes. Improve this answer. Community Bot 1 1 1 silver badge. It seems to me that your answer is just targeting my interviewer question. I am trying to re-iterate your reply with a practical example.
Please let me know, if it justifies your reply. Let us take an example of television. Abstraction is the on-off, sound, channel setting and other buttons provided to operate it. Guys abstraction has nothing to do with hiding implementation Mortalus - I don't agree your 1st statement "abstraction has nothing to do with hiding implementation". I think, when we start learning Abstraction concept the first thing that we are being taught is that we hide complexity of the implementation and provide some interface to user to use it.
Because user may not be interested in implementation complexity. You can take a practical example of TV or AC. The another thing is that if it does not hide implementation then why we provide a method in abstract class that do not have body. We are clearly hiding the implementation here. Mortalus - Also, you mentioned a wiki link. It's first statement - "In computer science, abstraction is the process by which data and programs are defined with a representation similar in form to its meaning semantics , while hiding away the implementation details.
Abstraction tries to reduce and factor out details so that the programmer can focus on a few concepts at a time. I am agree with your 2nd statement but not with 1st. I think Encapsulation is just more about building a single unit, its not much about you hide it or not.
Where as Abstraction is more about hiding things, please correct if i am wrong. Show 2 more comments. This image sums pretty well the difference between both: Source here. ToString ; Here, ToString is abstraction. Pang 8, gold badges 82 82 silver badges bronze badges. Arif Arif 4, 3 3 gold badges 43 43 silver badges 67 67 bronze badges. Mortalus Mortalus Let's dig into why I am saying this. I need to clear two things before my claim. One is data hiding and, another one is the abstraction Data hiding Most of the time, we will not give direct access to our internal data.
Our internal data should not go out directly that is an outside person can't access our internal data directly. It's all about security since we need to protect the internal states of a particular object. Abstraction For simplicity, hide the internal implementations is called abstraction. In abstraction, we only focus on the necessary things.
Basically, We talk about "What to do" and not "How to do" in abstraction. Security also can be achieved by abstraction since we are not going to highlight "how we are implementing". We can call this abstraction as we are abstracting the tasks. Here we saw the example of Agile and waterfall project and in future, we can have some more types and specific implementation of the methods.
WaterFallProject class has PerpareDocument method which calls two private methods to get different required documents and has a different implementation of DoProgramming method. Any consumer to these class will never to know how their documentation is done. Here, we are encapsulating those functionalities, which are not significant for the external consumer.
I hope this article is your ultimate and one-stop search for the abstraction vs encapsulation topic. Please feel free to give feedback to improve. View All. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Cyber Security. C Programming. Control System. Data Mining.
Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services.
0コメント