In this tutorial, we will learn about class and objects in Kotlin. As we know Kotlin is a JVM based programming language and same as Java, Kotlin is an Object-Oriented Programming language. In simple words, Class is a blueprint for an object. For example, you have object student we can create a class for the student object which can have variables like stu_name, stu_class and some methods etc. We can create any number of objects of a class. The way to create a class in Kotlin is same as Java. Please check out the following Syntax to create a class in Kotlin.
Page Contents
class CLASS_NAME {
// class Body
}
Creating a Class:
Same as Java there is a class keyword in Kotlin. Which we used to create a class in Kotlin. This class keyword followed by Class_Name. we can write any number of variables and methods in class according to requirements. We can also control the visibility of the class members variables and methods using access specifiers. To understand this wisely please check out the following example program in which we will create a Student class.
Example Program:
class Student {
// property (data member)
private var name: String = "Ram"
// member function
fun showInfo(){
print("Name: "+name)
}
}
Creating an Object:
We can write any number of objects of the class. It is simple to create an object of the class in Kotlin. Please check out the following example program.
class Student {
// property (data member)
private var name: String = "Ram"
// member function
fun showInfo(){
print("Name: "+name)
}
}
fun main(args: Array<String>) {
val obj = Student() // creating obj object of Student class
obj.showInfo()
}
Output:
Name: Ram
Parvesh Sandila is a results-driven tech professional with 8+ years of experience in web and mobile development, leadership, and emerging technologies.
After completing his Master’s in Computer Applications (MCA), he began his journey as a programming mentor, guiding 100+ students and helping them build strong foundations in coding. In 2019, he founded Owlbuddy.com, a platform dedicated to providing free, high-quality programming tutorials for aspiring developers.
He then transitioned into a full-time programmer, where his hands-on expertise and problem-solving skills led him to grow into a Team Lead and Technical Project Manager, successfully delivering scalable web and mobile solutions. Today, he works with advanced technologies such as AI systems, RAG architectures, and modern digital solutions, while also collaborating through a strategic partnership with Technobae (UK) to build next-generation products.
