What is the difference between abstraction and encapsulation. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

What is the difference between abstraction and encapsulation.

13th Jun 2022, 5:13 AM
Ajmal K saleem
Ajmal K saleem - avatar
4 Answers
+ 2
"Data abstraction is the process of hiding certain details and showing only essential information to the user" Abstract class can't be instancied, you have to make an instance of his child. https://www.tutorialspoint.com/java/java_abstraction.html An exemple with a code using decorator pattern https://code.sololearn.com/c1Pxa0p2ASAo/?ref=app Encapsulation his the principle of data hidding. Class attributs are private, Access is via getters and setters where it makes sense to expose parameters. It is also possible to add conditions in these methods. https://www.tutorialspoint.com/java/java_encapsulation.html Exemple with Builder pattern https://code.sololearn.com/cbHwnPkFyKsK/?ref=app
13th Jun 2022, 6:34 AM
Roland - avatar
Abstraction is the method of hiding the unwanted information. Whereas encapsulation is a method to hide the data in a single entity or unit along with a method to protect information from outside. Looking for more info about anything? Try a Search Engine.
13th Jun 2022, 6:13 AM
Chris Coder
Chris Coder - avatar
Both are oop topics Abstraction is a form of inheritance Example abstract class shape Has a function getArea() that returns a number This class can then be extended in another class class Rectangle extends shape { @override int getArea(int l,int w){ return l*w; } } Encapsulation has to do with how data of an object is access through out the program an example is public or private . When creating a variable and setting its access modifier to private disables other obects of different classes from being able to modiy its value . Public methods can be called outside of its class in another class and the private methods can only be called within its own class
25th Jun 2022, 4:01 AM
joshua - avatar
Another class circle can also extend shape A requirement when extending an abstract class is that all methods defined in that class must be overridden Another thing to keep in mind a class can only extend one more class but can implement many interfaces
25th Jun 2022, 4:06 AM
joshua - avatar