Introduction
In this blog, you will learn about classes and objects in Kotlin. Kotlin, like other programming languages, supports basic programming paradigms like Object Oriented Programming (OOP) and functional programming. The concept of OOPs is derived from the idea of objects and classes. With the use of classes and objects in Kotlin, one can achieve the fundamental principles of OOPs like abstraction, encapsulation, inheritance and polymorphism. In the next section, we will study about Kotlin Classes.
Kotlin Class
Before diving deep, let us understand the meaning of the term class. A class is a template for objects with similar attributes. Kotlin classes are identical to the class definition in Java and are declared using the keyword class.
Syntax of Kotlin class declaration
A Kotlin class has a class header that describes the type of arguments, constructors, and a class body enclosed by curly brackets. To learn more about Kotlin constructors, refer to the blog Kotlin Constructors on the Coding Ninjas Website.
class TestClass{ // class header
// class variables and member function with appropriate access modifiers
}
Example of Kotlin class
The following is an example of a Kotlin class. The class name is Student, and it has a secondary constructor that initialises two of its arguments, i.e. ninja_id and student_name. The class also contains two member functions named get_course_list and get_CGPA, which do their respective work.
Code:
// Kotlin program to demonstrate class syntax with example
class Student {
constructor (ninja_id: Int, student_name: String) {
println("Class Constructor --> Ninja ID: ${ninja_id} and Student Name: ${student_name}")
}
fun get_course_list() {
// Get all the courses in which the student with given ninja_id is enrolled in
}
fun get_CGPA() {
// Get CGPA of the student with given ninja_id
}
}




