Ideas are easy. Implementation is hard. Be Passionate.

Basic of Kotlin to get started in android Part-3

In my last tutorial, we understood basic introduction about Kotlin and basic features to get a quick start building an Android application. In this series, we will learn about of Classes and Objects in Kotlin. Sounds good 🙂

Classes and Objects in kotlin are almost similar to Java. It contains the access modifiers and constructor. But one thing in Kotlin is that Fields has no more with Kotlin. Kotlin classes are all about the properties. And properties are initialized before the constructor.

If we are talking about Constructor then Kotlin has updated constructor schema. Now in Kotlin Constructor will be two different types. One is Primary Constructor and other is Secondary Constructor.

The class can declare secondary constructors, which are prefixed with the constructor. but it is not required for a primary constructor. We are very much familiar with the access modifiers in Java but in Kotlin it is the little bit modified. Ok, Let’s check what are the visibility modifiers available in Kotlin.

Kotlin has four visibilities:

1. private
2. protected
3. internal
4. public

If we do not declare any visibility modifier, the public is used by default, which means that your declarations will be visible everywhere.

If we declared access modifier as a private, then it will only be visible in the class. If we declare access modifier as internal then it is visible everywhere in the same module.

Protected is not available for top-level declarations. It can access in the same class and their subclasses.

Let’s see the example that how we can use classes in kotlin?

Java

1

2

3

4

5

6

7

8

9

10

classTestClass(privatevarcontext:Context){

init{

getAppName()

}

privatefun getAppName(){

val appName=context.getString(R.string.app_name)

print("App Name is: $appName")

}

}

Here init is the initializer block to initialized anything which is required for a class. Let’s see how can we write the primary and secondary constructor in kotlin.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// primary constructor

classPerson1(name:String){

init{

println("My name is : $name")

}

}

// primary constructor

classPerson2(varname:String){

// secondary constructor

constructor(name:String,parent:Person2):this(name){

print("constructor is: $name")

}

init{

println("init block called")

}

}

Generic Class: Generic class could be any type of Object. Let’s see how can we create generic type class and objects.

Java

1

2

3

4

5

6

7

8

9

// generic type

classBox<T>(t:T){

varvalue=t

}

// T will be any type of Object ex String , Int or Object

varobj=Box(1);

varobj2=Box("Name")

varobj3=Box(Maruti("Maruti-800","Blue"))

Nested Class: A class which is inside the class is called nested class. We know very much clear in Java. The things are similar to Kotlin also. Let’s see how can we create the nested class and their object in Kotlin?

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// nested class

classA{

privateval nameTitle:String="Kotlin"

fun getName():String{

returnnameTitle;

}

classB{

fun getObjectName():String{

varobjName="New Kotlin"

returnobjName;

}

}

}

// create the object and get the data

varobjValue1=A().getName();// it will give "Kotlin"

varobjValue=A.B().getObjectName()// it will get "New Kotlin'

Inner Class: Inner class will be prefixed with an inner keyword.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// Inner class

classC{

privateval nameTitle:String="Kotlin"

fun getName():String{

returnnameTitle;

}

innerclassD{

fun getObjectName():String{

varobjName="New Kotlin"

returnobjName;

}

}

}

varobjInnerValue1=C().getName()// Kotlin

varobjInnerValue2=C().D().getObjectName()// new Kotlin

Anonymous Inner Class: A class which does not have any name is called anonymous class. Let’s see how can we write an anonymous class in Kotlin?

If I explain what are those in Kotlin before we need to understand the same thing in Java. I guess everyone knows about the abstract class and data class (POJO) and inheritance. I want to remind all those in simple java example.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

publicabstractclassCar{

privateStringname;

Car(Stringname){

this.name=name;

}

}

publicclassMarutiextendsCar{

privateStringmodel;

privateStringcolor;

Maruti(Stringmodel,Stringcolor){

super("Maruti");

this.model=model;

this.color=color;

}

}

How can we create the object of an abstract class? It can’t because Car class is an abstract class and we can not create an object of abstract class but when we want to use this class we need to inherit from other class and instantiate this class.

Java

1

Maruti focus=newMaruti("maruti 800","blue")

If I create the same thing in Kotlin then It is pretty simple and easy. Let’s see.

Java

1

2

3

4

5

6

7

8

abstractclassCar(val name:String){}

classMaruti(val model:String,val color:String):Car("Maruti")// -> here Maruti is inherited from Car, So Maruti can access the all property of car.

val maruti=Maruti("Maruti-800","Blue")

// if we want to create a new Object of Maruti with property name and value

val maruti2=Maruti(model="Maruti-800",color="Green")

Maruti class is inherited from Car class, So Maruti can access all property of Car. The car has one open method, So whenever Maruti class inherit Car class then need to override this method or function. Let’s see with an example what am I talking about?

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

abstractclassCar(val name:String){

fun getName(){

}

open fun getCarName():String{

returnname;

}

}

classMaruti(val model:String,val color:String):Car("Maruti"){

override fun getCarName():String{

returnsuper.getCarName()

}

}

// creating object

val maruti=Maruti("Maruti-800","Blue")

Wrapping up: we understood that Kotlin is an awesome language which is taken a good part of Java. Let’s make this language more powerful to build an awesome android app by using Kotlin. You can check the official website to get more details.

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Categories

For better assist

Hey Folks, Welcome to the MobologicPlus blog. I’m Sunil Gupta, a tech blogger from Bangalore, India.
I started MobologicPlus as a passion, by helping people to understand the technical kinds of stuff from this blog. You have some feedback for MobologicPlus. Where should I send it? Sure, drop us a line anytime at contact@mobologicplus.com