종우의 컴퓨터 공간
What is encapsulation? 본문
What Is Encapsulation?
Encapsulation is one of the fundamental concepts in object-oriented programming. It describes the idea of bundling(grouping) data and methods that work on that data within one unit such as classes in Java.
This concept is also often used to hide the internal representation, or state, of an object from the outside. This is called information hiding. The general idea of this mechanism is simple. If you have an attribute that is not visible from the outside of an object, and bundle it with methods that provide read or write access to it, then you can hide specific information and controll access to the internal state of the object.
Access Modifiers
Java supports four access modifiers that you can use to define the visibility of classes, methods, and attributes. Each of them specifies a different level of accessbility, and you can only use one modifier per class, method, or attribute. As a rule of thumb, you should always use the most restrictive modifier that still allows you to implement your business logic.
These modifiers are, starting from the most to the least restrictive one:
· Private: if you use the private modifier with an attribute or method, it can only be accessed within the same class.
· No modifier: when you don't provide any access modifier for your attribute or method, you can access it within your class and from all classes within the same package.
· Protected: attributes and methods with the access modifier protected can be accessed within your class, by all classes within the same package, and by all subclasses within in the same or other packages.
· Public: methods and attributes that use the public modifier can be accessed within your current class and by all other classes.
Reference
https://stackify.com/oop-concept-for-beginners-what-is-encapsulation/
OOP Concept for Beginners: What is Encapsulation
Encapsulation is one of the fundamental concepts in OOP. It describes the idea of bundling data and methods that work on that data within one unit.
stackify.com
'Preparing Interviews > Object Oriented Programming' 카테고리의 다른 글
What is abstraction? (0) | 2021.09.24 |
---|---|
What is Object Oriented Programming(OOP)? (0) | 2021.09.21 |