October 2018
Intermediate to advanced
370 pages
9h 15m
English
Kotlin provides another modifier, which is less restrictive as compared to the private modifier but very important when it comes to class hierarchy. This is called the protected modifier. Class properties and functions that are declared protected are accessible only in child classes. Take a look at the following example.
Create class A with two properties, i with protected visibility and j with public visibility. Create a child, class B, and inherit it with class A. Create a display function in class B and print all properties of class A in it:
class A { protected val i = 1 public val j = 2}open class B : A() { fun display(){ println("Protected i $i" ) println("Public j $j") }}fun main(args: Array<String>) { val ...Read now
Unlock full access