Junior — Middle
What aspect of OOP does the structure of a class or object reflect?
sobes.tech AI
Answer from AI
The structure of a class or object reflects an aspect of OOP called encapsulation. Encapsulation combines data (fields) and methods that work with this data into a single entity — a class or object. This allows hiding internal implementation details and managing access to them through an interface (methods).
In Go, although there are no classes in the traditional sense, structures (struct) and methods attached to them implement encapsulation by combining data and behavior.
Example in Go:
Type User struct {
name string
age int
}
func (u *User) GetName() string {
return u.name
}
func (u *User) SetName(name string) {
u.name = name
}
Here, the User structure encapsulates data and provides methods to access it.