Created
June 9, 2023 05:37
-
-
Save kalaiselvan369/1c93823b0b07fe71973440778791eac4 to your computer and use it in GitHub Desktop.
Constructor definition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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