Skip to content

Instantly share code, notes, and snippets.

@kalaiselvan369
Created June 9, 2023 05:37
Show Gist options
  • Save kalaiselvan369/1c93823b0b07fe71973440778791eac4 to your computer and use it in GitHub Desktop.
Save kalaiselvan369/1c93823b0b07fe71973440778791eac4 to your computer and use it in GitHub Desktop.
Constructor definition
fun main() {
// constructor is a special type of member function that instantiate the class
val elephant = Elephant()
println(elephant)
val employee = Employee("Ram")
println(employee)
val student = Student("Arun")
println(student.toString())
println(student.profile)
val worker = Worker("Tony")
println(worker.name)
println(worker.toString())
}
// constructor without parameter
class Elephant
// constructor with parameter
class Employee(name: String)
// constructor parameter accessible only to the member property
class Student(name: String) {
val profile: String = "Student name is $name"
}
// constructor parameter accessible outside the class body
class Worker(val name: String) {
// override default toString() with our own implementation
override fun toString(): String {
return "Worker Class"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment