Kotlin – Visibility and Inheritance Modifiers

Kotlin – Visibility and Access Modifiers

Visibility modifier Visibility modifier defines the scope from where it is visible and can be accessed Classes, objects, functions, interfaces and properties. Kotlin has 4 modifiers: public (it is the default one), private, protected and internal. A class can be accessed from everyone with public modifier. It is the default modifier so you can skip public. public class Car(_name:String ){ var name:String = _name get() { print(“Getter”) return field } set(value) { print(“Setter”) field = value } val description:String = “This is a $name car”} private modifier allows the object…

Kotlin – Visibility and Inheritance Modifiers Read More

Kotlin – Data Classes

Kotlin – Data Classes

If you want to create that it’s only purpose is to hold data, then Data class is for you. You can declare a data class this way: Data classes should have one parameter at the primary constructor and use val or var, cannot be open, abstract,inner or sealed and can only implement interfaces. Data class has the following members: You can read more here. Kotlin – Classes Part 3 Properties Kotlin – Number conversion Kotlin – Classes Part 2 Inheritance Kotlin – Data Classes

Kotlin – Data Classes Read More

Kotlin – Classes Part 3 Properties

Kotlin – Classes Part 3 Properties

Declaring Properties Mutable properties is declared using var and read-only with val You can simplify the above example by declaring the parameter in the primary constructor with the var keyword If you bypass Kotlin’s automatic process for getters and setters, you must not declare the property in the primary constructor and write your own getter and setter method. The syntax is Accessing Properties If you want to access a class property then you use . and the name of the property fun main(args: Array){ var toyota = Car(“Toyota”) toyota.name =…

Kotlin – Classes Part 3 Properties Read More

Kotlin – Number conversion

In Kotlin if you want to convert a type of number to another type, for example integer to long, you need to use the following functions: The above functions are supported from Short, Byte, Int, Long, Double and Float. Kotlin – Classes Part 2 Inheritance Kotlin – Classes Part 1 declaration and constructors Kotlin – Interfaces Kotlin – Number conversion

Kotlin – Number conversion Read More

Kotlin – Classes Part 2 Inheritance

Screenshot 20190123 133806

Let’s say we have the following class: If you want to learn more about the code above, read Part 1 Now, if you want this class to be inherited or extended, you need to tell that to Kotlin. You can do that with open. In Kotlin all the classes are final. That means that the classe cannot be subclassed. Now you can extend or inherit this class You need to initialize the class here because it has a primary constructor. That is why Suv class has one parameter, and pass…

Kotlin – Classes Part 2 Inheritance Read More