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 🙂

But before if you have not checked my last tutorial then I would be recommended checking my all post for better understanding which is based on basic of Kotlin to get started in Android part series. Here is the link of Basic of Kotlin to get started in Android part-1 and  Basic of Kotlin to get started in Android part-2.

Classes and Objects

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?

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.

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

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?

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

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?

Abstract, Data class and Inheritance:

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.

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.

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

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?

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.

Ok, In my next tutorial, we will learn more features of Kotlin.

So please do subscribe your email to get the newsletter on this blog and if you feel this post is good then do not forget to share and comment below.
Happy Coding 🙂

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

Leave a Reply

Your email address will not be published. Required fields are marked *