Kotlin - Class and Object

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. 

While a class is the definition, objects are real. Objects have data and are stored in computer memory. We can create an object, Gandhi and Bachchan, where both are the objects of type Person class. They both have properties such as skin color, hair color, height, weight, age, or living or not. In this case, Gandhi's living property is false and Bhachchan's is true. In this case, Gandhi's functions, Eat, Sleep, and Run don't return any data while Bachchan's do. 
 
Similarly, in our software applications, an architect or system designer will need to figure out how many and what types of classes he/she needs to create to define possible objects in the software. For example, a home builder system may have classes such as a Building, House, Room, Bathroom, Kitchen, and so on. Each of these classes will have their properties and functions. 
 
Now, let's see how classes and objects can be created and used in real software programs. 
 
Declaring a clsss 
 
Kotlin is used to declare keyword class. Kotlin Class is used to specify its type parameter and constructor and it is surrounded with curly braces

Syntax

  1. class className{  
  2.   
  3. //Property  
  4.   
  5. //function  
  6.   
  7. }   

 

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

  1. var obj1 =className()  

 

Kotlin Nested Class

Nested class is used to create inside another class.Nested class cannot access the outer class.

Syntax

  1. class outerClass{  
  2.   
  3. //outer class code  
  4.   
  5. class nestedClass{  
  6.   
  7. //nested class code  
  8.   
  9. }  
  10.   
  11. }  

 

Example

  1. class outerClass{  
  2.   
  3. private var name: String = "Abu Shithik"  
  4.   
  5. class nestedClass{  
  6.   
  7. var description: String = "kotlin 13 articles"  
  8.   
  9. private var id: Int = 121  
  10.   
  11. fun csharp(){  
  12.   
  13. // print("name is ${name}")  
  14.   
  15. println("Id is ${id}")  
  16.   
  17. }  
  18.   
  19. }  
  20.   
  21. }  
  22.   
  23. fun main(args: Array<String>){  
  24.   
  25. println(outerClass.nestedClass().description)  
  26.   
  27. var obj = outerClass.nestedClass()  
  28.   
  29. obj.csharp()  
  30.   
  31. }  

 

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

  1. class outerClass{  
  2.   
  3. inner class innerClass{  
  4.   
  5. }  
  6.   
  7. }   

 

Example

  1. class outerClass{    
  2.   
  3. private var name: String = "Abu shithik"    
  4.   
  5. inner class innerClass{    
  6.   
  7. var description: String = "code inside inner class"    
  8.   
  9. private var id: Int = 122    
  10.   
  11. fun csharp(){    
  12.   
  13. println("name is ${name}")   
  14.   
  15. println("Id is ${id}")    
  16.   
  17. }    
  18.   
  19. }    
  20.   
  21. }    
  22.   
  23. fun main(args: Array<String>){    
  24.   
  25. println(outerClass().innerClass().description)   
  26.   
  27. var obj = outerClass().innerClass()   
  28.   
  29. obj.csharp()   
  30.   
  31. }   

 

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

  1. abstract class Abu {  
  2.   
  3. var q = 0  
  4.   
  5. abstract fun doSomething()  
  6.   
  7. }  

 

Example

  1. abstract class Csharp{  
  2.   
  3. abstract fun article()  
  4.   
  5. }  
  6.   
  7. class blog: article(){  
  8.   
  9. override fun article(){  
  10.   
  11. println("Article is not getting Approval..")  
  12.   
  13. }  
  14.   
  15. }  
  16.   
  17. fun main(args: Array<String>){  
  18.   
  19. val obj = blog()  
  20.   
  21. obj.article();  
  22.   
  23. }  

 

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. 

Constructors
 
A constructor
 
Properties
 
A property
 
Functions 
 
A function  
 

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!


Similar Articles