Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Kotlin break the rule of encapsulation?

Variables are used public in classes using the default visibility modifier. A setter and a getter is created for every member variable, but in Kotlin you do for example:

class Person {
    var name: String = "unknown"
}

fun main(args: Array<String>) {
    val person = Person()
    person.name = "kevvex"
    println("${person.name}")
}

Is this still not breaking the rule of encapsulation due to the getter and setter being applied when using:

person.name = "kevvex"

If so, how can that be encapsulation? The variable is still public. Declaring it as private would force me to create a setter and getter to get the variable, because of the private visibility modifier.

I'm comparing to Java that often has member variables as private and a public setter and getter for every member variable.

like image 929
kevvex Avatar asked Jul 05 '18 13:07

kevvex


People also ask

Does kotlin have encapsulation?

Kotlin is an object-oriented, statically typed, programming language. This means we use interfaces, classes, and abstract classes (among some other unique data structures) to define our data and objects. Encapsulation in our classes serves 2 primary purposes – Code security and architecture strength.

What is a property Kotlin?

Properties. Properties are the variables (to be more precise, member variables) that are declared inside a class but outside the method. Kotlin properties can be declared either as mutable using the “var” keyword or as immutable using the “val” keyword.

What is encapsulation in Java?

Encapsulation in Java is the process by which data (variables) and the code that acts upon them (methods) are integrated as a single unit. By encapsulating a class's variables, other classes cannot access them, and only the methods of the class can access them.


2 Answers

I'm comparing to Java that often has member variables as private and a public setter and getter for every member variable.

This is actually what is happening in this Kotlin code. name is not a field (what you call a member variable), it's a property with a private backing field. And if you modify its getter and setter:

var name: String
    get() = ...
    set(value: String) { ... }

the callers continue using

person.name = "kevvex"

and don't require recompilation.

I.e. var name: String = "unknown" is exactly equivalent to Java's

private String _name = "unknown";
public String getName() { return _name; }
public void setName(String name) { this._name = name; }

and will even be seen from Java like that. So it breaks encapsulation to the same degree Java does.

like image 111
Alexey Romanov Avatar answered Oct 08 '22 14:10

Alexey Romanov


You can create public variable but only with private setter

var name: String = "unknown"
private set

Additionaly you can edit how your get or set behaves, just like in Java. There is no problem with encapsulation, Kotlin makes "POJOs" creation much easier by creating "default setters / getters" as public ones

like image 34
Janusz Hain Avatar answered Oct 08 '22 14:10

Janusz Hain