- 1
What is the difference between abstraction and encapsulation.
4 Réponses
+ 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
0
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.
0
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
0
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