0% found this document useful (0 votes)
67 views

Kotlin OOP Presentation

This is related to Mobile application development. Topic is oop in Kotlin.

Uploaded by

hs6515700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Kotlin OOP Presentation

This is related to Mobile application development. Topic is oop in Kotlin.

Uploaded by

hs6515700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

OOP in Kotlin

• Object-Oriented Programming (OOP) in Kotlin is based on objects and classes.


• It follows the four main principles: Encapsulation, Inheritance, Polymorphism, and
Abstraction.
• Kotlin’s OOP is similar to other OOP languages but has unique features like data classes,
extension functions, and more.
Class in Kotlin
• In Kotlin, a class is defined using the 'class' keyword, and it can have properties, methods, and
constructors.
• Example:
• class Person(val name: String, var age: Int) {
• fun greet() {
• println("Hello, my name is $name and I am $age years old.")
• }
• }

• fun main() {
• val person = Person("Ali", 21)
• person.greet()
• }
Function in Kotlin
• A function in Kotlin is defined using the 'fun' keyword.
• Example:
• fun add(a: Int, b: Int): Int {
• return a + b
• }

• fun main() {
• val sum = add(5, 3)
• println("Sum: $sum")
• }
Constructor in Kotlin
• Kotlin has primary and secondary constructors.
• The primary constructor is part of the class header, while secondary constructors are inside
the class body.
• Example:
• class Student(val name: String, val age: Int) { // Primary constructor

• init {
• println("Student initialized with name: $name and age: $age")
• }

• // Secondary constructor
• constructor(name: String) : this(name, 18) {
• println("Secondary constructor called")
• }
• }

• fun main() {
• val student1 = Student("Ali", 21)
Object in Kotlin
• In Kotlin, an object can be created using the 'object' keyword for a singleton pattern or for
creating an anonymous object.
• Example of Singleton:
• object Database {
• val name = "MyDatabase"

• fun connect() {
• println("Connecting to $name")
• }
• }

• fun main() {
• Database.connect()
• }
Dialog Box in Kotlin
• A dialog box in Android can be created using AlertDialog.Builder.
• Example:
• import android.app.AlertDialog
• import android.content.Context

• fun showDialog(context: Context) {


• val builder = AlertDialog.Builder(context)
• builder.setTitle("Dialog Title")
• builder.setMessage("This is a dialog message.")
• builder.setPositiveButton("OK") { dialog, _ ->
• dialog.dismiss()
• }
• builder.setNegativeButton("Cancel") { dialog, _ ->
• dialog.dismiss()
• }
• val dialog = builder.create()
• dialog.show()
• }
LayoutInflater in Kotlin
• LayoutInflater is used to inflate a layout XML file into its corresponding view objects in Kotlin.
• Example:
• import android.view.LayoutInflater
• import android.view.View
• import android.view.ViewGroup

• fun inflateView(inflater: LayoutInflater, parent: ViewGroup): View {


• return inflater.inflate(R.layout.custom_layout, parent, false)
• }
• This creates a view object from the custom_layout.xml file and can be used in activities or
fragments.

You might also like