In this tutorial, we will learn all about Classes and Objects in Kotlin with examples.
Kotlin- Create Class Example
package net.javaguides.kotlin.examples
class User {
// Properties or Member Variables
var firstName: String;
var lastName: String;
// Secondary Constructor
constructor(firstName: String, lastName: String) {
this.firstName = firstName
this.lastName = lastName
}
}
fun main(args: Array < String > ) {
val person = User("Tony", "Stark")
println(person.firstName) // Tony
println(person.lastName) // Stark
}Tony
StarkKotlin Class Functions
class Car(var brand: String, var model: String, var year: Int) {
// Class function
fun drive() {
println("Wrooom!")
}
// Class function with parameters
fun speed(maxSpeed: Int) {
println("Max speed is: " + maxSpeed)
}
}
fun main() {
val c1 = Car("Ford", "Mustang", 1969)
// Call the functions
c1.drive()
c1.speed(200)
}
Output:
Ford Mustang 1969
Wrooom!
Max speed is: 200Kotlin Nested Class
A nested class is a class that is created inside another class. In Kotlin, the nested class is by default static, so its data member and member function can be accessed without creating an object of a class. The nested class cannot be able to access the data member of the outer class.Kotlin Nested Class Example:
package net.javaguides.kotlin
class outerClass {
private
var name: String = "outerClass"
class nestedClass {
var description: String = "code inside nested class"
private
var id: Int = 101
fun foo() {
// print("name is ${name}") // cannot access the outer class member
println("Id is ${id}")
}
}
}
fun main(args: Array < String > ) {
// nested class must be initialize
println(outerClass.nestedClass().description) // accessing property
var obj = outerClass.nestedClass() // object creation
obj.foo() // access member function
}
Output:code inside nested class
Id is 101
Kotlin Abstract Class
Declaration of abstract class
abstract class A {
var x = 0
abstract fun doSomething()
} Kotlin Abstract Class Example
package net.javaguides.kotlin.examples
abstract class Bank {
abstract fun simpleInterest(principle: Int, rate: Double, time: Int): Double
}
class SBI: Bank() {
override fun simpleInterest(principle: Int, rate: Double, time: Int): Double {
return (principle * rate * time) / 100
}
}
class PNB: Bank() {
override fun simpleInterest(principle: Int, rate: Double, time: Int): Double {
return (principle * rate * time) / 100
}
}
fun main(args: Array < String > ) {
var sbi: Bank = SBI()
val sbiint = sbi.simpleInterest(1000, 5.0, 3)
println("SBI interest is $sbiint")
var pnb: Bank = PNB()
val pnbint = pnb.simpleInterest(1000, 4.5, 3)
println("PNB interest is $pnbint")
}SBI interest is 150.0
PNB interest is 135.0Kotlin Inheritance
open class Base(p: Int){ } class Derived(p: Int) : Base(p){ }
Kotlin Inheritance Example
package net.javaguides.kotlin.examples
open class Employee(name: String, age: Int, salary: Float) {
init {
println("Name is $name.")
println("Age is $age")
println("Salary is $salary")
}
}
class Programmer(name: String, age: Int, salary: Float): Employee(name, age, salary) {
fun doProgram() {
println("programming is my passion.")
}
}
class Salesman(name: String, age: Int, salary: Float): Employee(name, age, salary) {
fun fieldWork() {
println("travelling is my hobby.")
}
}
fun main(args: Array < String > ) {
val obj1 = Programmer("Ramesh", 25, 40000 f)
obj1.doProgram()
val obj2 = Salesman("Vijay", 24, 30000 f)
obj2.fieldWork()
}Name is Ramesh.
Age is 25
Salary is 40000.0
programming is my passion.
Name is Vijay.
Age is 24
Salary is 30000.0
travelling is my hobby.Kotlin open keyword
As Kotlin classes are final by default, they cannot be inherited simply. We use the open keyword before the class to inherit a class and make it non-final.open class Employee(name: String, age: Int, salary: Float) { init { println("Name is $name.") println("Age is $age") println("Salary is $salary") } }
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment