Understanding Kotlin — Part 2
In this article we are going to how to declare the function and classes in kotlin and how it differs from declaring it in java. If you are new to kotlin please check out my previous article Part 1 here. Ok let’s get started to the topic.
Function Declaration :
In kotlin the function declaration is little bit different from declaring function in java. In kotlin we can declare the function using fun keyword. The basic function declaration should be starts with fun keyword and function name and parameters if necessary followed by return type. We can declare the function with normal curly braces {} or kotlin offers to assign the function with declarations.
String Interpolation :
Unlike java, want to concatenate more than one value we don’t need to use the plus(+) or concatenation operator. Kotlin offers to special character(${}) for the concatenationuse the “${value}” inside the double quotes we can concatenate values/variables with hard coded values.
In the above example we are getting the parameter value from function and printing the name parameter with some static string value. Here we are trying to use the special symbol to concatente. If we are concatenate the single argument we can use directly $name. We no need to make the curly braces, but it won’t show any error. Curly braces is mandatory for trying to access some variable from pojo classes. For eg if we have User model class with name and email, we can access as ${user.name}.
Function with arguments :
If we want to pass some arguments to the function, we first mention the variable name and colon(:) followed by variable type. Like wise the function is returning some value we can declare the return type using colon(:) with return type.
Class Declaration :
Class declarations are mostly similar like java. But when you try to implement the inheritance and try to extend one class with other the structure is differs slightly. You can find the sample code below.
While declaring the class is normal one like java. When extending the other class instead of using the “extends” keyword we are using “:”. We will extend the class MyWorld into YoursWorld with colon (:) and parent class name with constructor.
Shall we extend the one class easily?
So extending one class is not easy like java. When try to write the above code we will see some error. Because by default all the classes in kotlin is public and final and we can’t extend one class with others. There must be some rescue right. Yeah, and the rescue is the open keyword. If you declare your class with open, you can extend the class to other. This is how the inheritance is working in kotlin.
Data Classes :
Data classes is like normal classes which is used to avoid many boiler plate code for setter and getter of the variables. To avoid boiler plate codes in POJO(Plain Old Java Objects), kotlin offers this data classes. Data classes are similar to normal classes but the interesting thing is you need to add data keyword before the class keyword. Once you add this data keyword, whatever the variables you declare in constructor you can set and get easily without writing setter and getters. The data classes must have atleast one parameter in the constructor.
And some of the drawbacks are you can’t extend one data class with other and you can’t make your data class as open class.
Conclusion :
So from this article we come to know how to declare function, classes, data classes and string interpolation. Please do clap (👏) if you like this post. Thanks and Cheers.
#Kotlin #proggyblast #balajisb Balaji S B Proggy Blast