Beginning Kotlin by 2023
Author:2023
Language: eng
Format: epub
ChapteR 4 tYpes
â seeing the final and override keywords on the same line seems odd, but itâs perfectly legal. It means we are overriding the function and, at the same time, closing it for further inheritance. the final keyword in this function affects only subtypes of the employee class, but not the employee class itself.
â this statement wonât compile anymore. the talk() function was marked final in the supertype (employee class).
Properties
Properties are important for POJOs (Plain OldJava Objects). Theyâre useful for modeling domain objects. Typically (in Java), you create properties by defining member variablesâusually by following naming conventionsâ
and these variables are customarily prefixed by get and set. After that, youâll create accessor methods to get and set the values of the member variables.
Listing 4-18 shows a typical POJO in Java with getter and setter methods.
Listing 4-18. Person class in Java with a single property public class Person {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name
}
93
ChapteR 4 tYpes
public static void main(String []args) {
Person person = new Person();
person.setName("John Doe");
System.out.println(person.getName());
}
}
In the preceding example, we have class Person with just one property ( name). We did this by making the name variable private so we can control the variableâs state only via accessor methods. We had to do this kind of coding in Java because the language did not have native support for properties. We donât have to do this in Kotlin because it has language support for properties. Letâs rewrite an equivalent of the previous POJO
example in Kotlin (see Listing 4-19).
Listing 4-19. Person class with a single property
class Person(_name:String) { â
val name:String = _name â
}
fun main(args: Array<String>) {
var person = Person("John Smith")
println(person.name) â
}
â a constructor takes in a parameter; this lets us set the name of the object at the time of object creation.
â We can access constructor parameters in the class body.
â this statement may look like we are directly accessing the name member variable, but we are not. this statement actually calls the get accessor method.
Kotlin works in the background to provide automatic accessors and backing fields.
94
ChapteR 4 tYpes
We can further simplify the Person example; Listing 4-20
shows us how.
Listing 4-20. Simplified Person class
class Person(val name:String)
fun main(args: Array<String>) {
var person = Person("John Smith")
println(person.name)
}
The code above is the most concise way of defining a property in
Kotlin. Itâs also considered idiomatic. Notice the changes we made in the code.
⢠The parameter in the primary constructor now has a
val declaration which makes the constructor parameter
an immutable property. If you want to make a mutable
(read/write) property, use the var keyword instead.
⢠We no longer need to differentiate the identifier in
the constructor parameter with the member variable;
hence we dropped the leading underscore in the
_name variable.
⢠We can drop the entire class body since we no longer
need it. The class body only contains the code to
transfer the value of the constructor parameter to the
member variable. Since Kotlin will automatically define
a backing field for the constructor parameter, we donât
have to do anything anymore in the class body.
The code in Listing 4-20 shows the most
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(15941)
The Mikado Method by Ola Ellnestam Daniel Brolund(13207)
Hello! Python by Anthony Briggs(13034)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(12219)
Dependency Injection in .NET by Mark Seemann(12064)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(10838)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(10657)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10537)
Grails in Action by Glen Smith Peter Ledbrook(10135)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(10023)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(9479)
Hit Refresh by Satya Nadella(9040)
Kotlin in Action by Dmitry Jemerov(8740)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(8646)
The Kubernetes Operator Framework Book by Michael Dame(8484)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8306)
Robo-Advisor with Python by Aki Ranin(8261)
Practical Computer Architecture with Python and ARM by Alan Clements(8232)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(8202)