Introduction
In this part, we are going to learn about Kotlin Class and Object. If you're new to Kotlin and willing to learn, please start here, Introduction to Kotlin.
Kotlin Class and Object
Kotlin is a modern programming language and supports both Object Oriented Programming and Functional Programming language features. Being an OOP language, Kotlin supports all mahor pillars of OOP’S including abstraction, encapsulation, inheritance, and polymorphism. If you're not familiar with OOP and its four pillars, start here: Introduction to Object Oriented Programming.
Kotlin Class
Classes are the center of the universe in Kotlin. A class is a type that defines the types of objects and their members. A class is a definition of type of objects. For example, a Person class is the definition of Person types. A Person class can have members such as sex, skin color, hair color, height, weight, age, or living. These members are called properties of a class. A class can also have other members such as functions. A function is something what class can do. For example, Eat, Sleep, and Run can be the functions of a Person class.
Syntax
- class className{
- //Property
- //function
- }
Kotlin Object
Kotlin Object which is used for real time entity with its characteristics such as state and behaviour.
State- value of an object
Behavior – functionality of an object
Example
- var obj1 =className()
Kotlin Nested Class
Nested class is used to create inside another class.Nested class cannot access the outer class.
Syntax
- class outerClass{
- //outer class code
- class nestedClass{
- //nested class code
- }
- }
Example
- class outerClass{
- private var name: String = "Abu Shithik"
- class nestedClass{
- var description: String = "kotlin 13 articles"
- private var id: Int = 121
- fun csharp(){
- // print("name is ${name}")
- println("Id is ${id}")
- }
- }
- }
- fun main(args: Array<String>){
- println(outerClass.nestedClass().description)
- var obj = outerClass.nestedClass()
- obj.csharp()
- }
Output
Kotlin 13 articles
Id is 121
Kotlin Inner Class
Inner class is used to create inside another class like nested class using keyword inner.
Syntax
- class outerClass{
- inner class innerClass{
- }
- }
Example
- class outerClass{
- private var name: String = "Abu shithik"
- inner class innerClass{
- var description: String = "code inside inner class"
- private var id: Int = 122
- fun csharp(){
- println("name is ${name}")
- println("Id is ${id}")
- }
- }
- }
- fun main(args: Array<String>){
- println(outerClass().innerClass().description)
- var obj = outerClass().innerClass()
- obj.csharp()
- }
Output
code inside inner class
name is Abu shithik
Id is 122
Kotlin Abstract Class
A class which is used to declare with keyword abstract is known as abstract class. The method and properties are used to declare with keyword abstract only known as Abstract.

Syntax
- abstract class Abu {
- var q = 0
- abstract fun doSomething()
- }
Example
- abstract class Csharp{
- abstract fun article()
- }
- class blog: article(){
- override fun article(){
- println("Article is not getting Approval..")
- }
- }
- fun main(args: Array<String>){
- val obj = blog()
- obj.article();
- }
Output
Article is not getting approval.
Class Members
A class can have several members types and each of the class members including constructors, properties, and functions have different roles to play in a class.
Conclusion
Kotlin is quickly becoming a complete modern programming language. In this article, we learned about classes in Kotlin. In the next part of this series, we will learn about Inheritance and Extension Functions. Stay tuned!

Join the conversation! Your thoughts help the community grow.