The Programmers Guide To Kotlin - The Class & The Object |
Written by Mike James | |||||||
Tuesday, 29 October 2024 | |||||||
Page 1 of 6 Kotlin is a class-based, object-oriented language and it works in a way that is compatible with Java objects. This doesn't mean that Kotlin does classes in exactly the same way as Java or any other language. In fact. part of the problem in getting to grips with Kotlin classes is that initially they look as if they are just like Java classes - but they aren't. They may be compatible with Java classes, but Kotlin offers some significant novel approaches to objects. Programmer's Guide To Kotlin Third Edition
You can buy it from: Amazon Contents
<ASIN:B0D8H4N8SK> ClassesDespite initial appearances, Kotlin is a full object-oriented language. Even though you can have functions at the package level, functions which appear not to be methods, these are in fact bundled up into the methods of a default class. In Kotlin everything is an object. Kotlin is fairly traditional in its approach to objects. It restricts inheritance to single inheritance and it supports interfaces with some inherited implementation to get around this restriction. It is also strongly-typed, despite managing to make code look as if type isn't important by using type inference. The first big shock is how easy it is to create a new class and an instance of it: class MyClass var myObject = MyClass() Notice that you don't need to use new, just use the class name as if it was a function, which of course it is, the constructor. A class doesn’t even need a body, although you can add a pair of empty curly brackets if you want to: class MyClass{} var myObject=MyClass() You can include a type specifier for clarity: var myObject:MyClass=MyClass() but this isn't the Kotlin idiom as type inference easily supplies the type. A Kotlin class can have properties and methods, i.e. properties that are functions. Both properties and methods are accessed in the usual way using the dot notation. For example if myMethod is a method that is defined in MyClass you can write: var myObject = MyClass() myObject.myMethod() Methods are just normal functions, but with the usual addition of this as a variable which references the current instance of the class. You can think of this as a default parameter which is passed to the method. That is: myObject.myMethod() is in principle equivalent to: myMethod(myObject) with the first parameter of the call being this. You can access properties in the same way, although it is worth keeping in mind that all Kotlin properties make use of getter and setter functions which are automatically generated if you do not supply them. That is, Kotlin classes do not have fields, i.e. member variables that are directly accessible outside of the class. A class often has multiple constructors to allow it to be customized in different ways. In other languages all of the constructors are generally treated as being of equal importance – with the possible exception of a single parameterless construction which can be called by the system to create a default instance. More importantly, they are all defined in the same way as methods within the body of the class declaration. This is not true in Kotlin. In Kotlin a class can have one primary constructor and as many secondary constructors as you like, and the primary constructor seems to be special in some way. The primary constructor can be a puzzle to programmers familiar with Java and other languages where constructors are simply methods that have the same name as the class. Kotlin does away with the redundancy of having to keep on stating the class name within the class definition. In Kotlin the class declaration is also the declaration of the primary constructor, and you can include parameters if you want to: class MyClass(count:Int = 0) The primary constructor is convenient but it is limited. You can't place any instructions in the primary constructor because it doesn't have a method body. That is, you can't write something like: class MyClass(count:Int){ if(count<0) count = 0; } The primary constructor has no method body and the body of the class declaration is for writing class members and secondary constructors. It is not the method body of the primary constructor, even though it might at first sight be mistaken for one. That is, the primary constructor is simply a list of parameters that can be passed when the class is instantiated. The question is, what can you do with these parameters if the primary constructor has no method body? The most you can do is to use the primary constructor’s parameters in property initializers: class MyClass(count:Int){ val max = count; } Notice that max is a property of MyClass and nothing to do with the primary constructor, except that it uses a parameter as an initializer. This is such a common idiom that Kotlin provides a way to link properties and the parameters in the primary constructor automatically. If you specify var or val in front of a parameter then it is converted into a read/write or read-only property, complete with initializers. For example: class MyClass(var count:Int = 0) has a read/write property called count initially set to 0. This idiom makes creating data-only classes, what would be called structs or records in other languages, easy. For example: class Person(val Name:String,var Address:String) creates a class with a Name and an Address property. Kotlin goes a little further than this in its support of data representation – see Data Classes in Chapter 12. At the moment it looks as if the primary constructor is limited, but later in this chapter we’ll see how adding an initializer block provides a way of executing some code independently of which constructor is called, and this includes the primary constructor. |
|||||||
Last Updated ( Tuesday, 29 October 2024 ) |