KEMBAR78
Introduction to kotlin and OOP in Kotlin | PPTX
Introduction to Kotlin
Compiler to Run Kotlin Programs
Hello World!
fun main() { // main function
//code starts here
println("Hello World!")
}
Data Types
● Int
● Char
● String
● Float
● Double
● Long
● Short
● Byte
● Boolean
Variables
● Mutable
Starts with ‘var’ and can be reassigned.
For ex:) var age = 20
● Immutable(Read only)
Starts with ‘val’ and cannot be reassigned.
For ex:) var name = “Jack”
Functions
Syntax:
fun func_name(){
//your code
}
fun sayHello() {
println("Hello")
}
fun main() { // main function
//code starts here
sayHello()
}
Examples
fun sayHello() {
println("Hello")
}
fun sayHelloWithName(name:String) {
println("Hello $name")
}
fun addTwoNumbers(x:Int, y:Int) {
print("The sum of the two numbers is ${x + y}")
}
Conditionals in Kotlin
(stuff you would use a lot while making apps)
Operators
if , else if, else
Basic Structure :
if( condition1 ){
// This stuff runs when Condition 1 holds
} else if( condition2 ){
// This runs when Condition 2 holds
} else if( condition3 ){
// This runs when Condition 3 holds
} else{
// If none of the condition holds, this runs
}
if , else if, else
Let’s Try One Example….
if , else if, else as Expression
Example dekhte hai direct…
when statement
//// Similar to switch in C++
//// Used instead of long if else ladder
fun main() {
val x = 2;
when(x) {
1 -> println("One")
2 -> println("Two")
3 -> println("Three")
else -> println("Number not recognized")
}
}
ranges
//// Collection of values from a START point to an END point
//// Similar to ranges in python
A range of number from 1 to 10 : 1..10
val x = 1..10;
A range of number from 10 to 1 : 10 downTo 1
val x = 10 downTo 1
for loop
//// Not like for loops in languages like C++ or Java
//// Used to iterate through a collection
Basic Structure :
for( item in collection ) {
// You could do something with item
}
Let’s look at few Examples…
-> with Ranges
-> with List
-> break, continue with labels
Quiz Time!!!
//// What will be the output of this?
a)
fun main() {
val x = 1..5
val y = 1..10
for( i in x ){
print("$i : ")
for( j in y ){
print("$j ")
if( i == 3 && j == 7 ) break
}
println()
}
}
b)
while Loop
//// Similar to the while loop in C++
//// Used for condition based looping
Basic Structure :
Let’s look at an Example…
while( condition ){
// Execute something
}
do while Loop
//// Runs the body of the loop at least ones even if the
condition doesn’t satisfies
Basic Structure :
Let’s look at an Example…
do{
// Body of the loop
}while ( condition )
repeat
//// Quite not found in any other languages
Basic Structure :
repeat(3) {
println("Hello")
}
Classes & Objects
Classes
Class is template of object
A class is a user defined blueprint or prototype
from which objects are created.
It represents the set of properties or methods that
are common to all objects of one type
Objects
Name : Eshan
Age : 20
Gender: Male
Occupation : Android Developer
Name : Priya
Age : 25
Gender : Female
Occupation : Doctor
Classes
Class : Car
Class is template of object
To Create a class ,
We use class Keyword
Properties of Car
Objects : Instance of Class
Object 1 of Class Car
Jaguar I-PACE Black
0 cc
Object 2 of Class Car
Tata Nano Yellow
624 cc
Create an Object
Constructor
objectName.DataMember
Output
Constructor
There is faster way of doing this
By using a CONSTRUCTOR
A constructor is like a special function, and it is defined by using two parantheses ()
after the class name. You can specify the properties inside of the parantheses (like
passing parameters into a regular function).
Constructor
Without Constructor
By Using Constructor
Properties
Note:
Remember to specify the type of the property/variable
Creating Object by using Constructor
Class Functions
Output
Object
https://pl.kotl.in/_ebphwnyK
Pillars of OOPs
Inheritance
In Kotlin, it is possible to inherit class
properties and functions from one class to
another
SubClass
Child
SuperClass
Parent
Inheritance
Parent
Use the open keyword in front of the superclass/parent, to make this the class other
classes should inherit properties and functions from.
Child
To inherit from a class, specify the name of the subclass, followed by a colon :, and then
the name of the superclass.
In Similar fashion we can inherit functions and other properties from parent to child
https://pl.kotl.in/_ebphwnyK
Polymorphism
Poly means many and morph means forms
i.e. the same method behaves differently based on the object
we have different forms of the
method that exhibits different
behavior based on the object.
Here we can see polymorphism of
area() Function
https://pl.kotl.in/_ebphwnyK
Abstraction
Abstraction is used to hide background details or any unnecessary
implementation about the data so that users only see the required
information.
It is one of the most important and essential features of object-oriented
programming.
Encapsulation
It is a protective shield that prevents the data from
being accessed by the code outside this shield.
Here we can’t access private properties of
class from outside the class
Abstraction
Encapsulation
Main function don’t know how these
setters getters work
Introduction to kotlin and OOP in Kotlin

Introduction to kotlin and OOP in Kotlin

  • 1.
  • 2.
    Compiler to RunKotlin Programs
  • 3.
    Hello World! fun main(){ // main function //code starts here println("Hello World!") }
  • 4.
    Data Types ● Int ●Char ● String ● Float ● Double ● Long ● Short ● Byte ● Boolean
  • 5.
    Variables ● Mutable Starts with‘var’ and can be reassigned. For ex:) var age = 20 ● Immutable(Read only) Starts with ‘val’ and cannot be reassigned. For ex:) var name = “Jack”
  • 6.
    Functions Syntax: fun func_name(){ //your code } funsayHello() { println("Hello") } fun main() { // main function //code starts here sayHello() }
  • 7.
    Examples fun sayHello() { println("Hello") } funsayHelloWithName(name:String) { println("Hello $name") } fun addTwoNumbers(x:Int, y:Int) { print("The sum of the two numbers is ${x + y}") }
  • 8.
    Conditionals in Kotlin (stuffyou would use a lot while making apps)
  • 9.
  • 10.
    if , elseif, else Basic Structure : if( condition1 ){ // This stuff runs when Condition 1 holds } else if( condition2 ){ // This runs when Condition 2 holds } else if( condition3 ){ // This runs when Condition 3 holds } else{ // If none of the condition holds, this runs }
  • 11.
    if , elseif, else Let’s Try One Example….
  • 12.
    if , elseif, else as Expression Example dekhte hai direct…
  • 13.
    when statement //// Similarto switch in C++ //// Used instead of long if else ladder fun main() { val x = 2; when(x) { 1 -> println("One") 2 -> println("Two") 3 -> println("Three") else -> println("Number not recognized") } }
  • 14.
    ranges //// Collection ofvalues from a START point to an END point //// Similar to ranges in python A range of number from 1 to 10 : 1..10 val x = 1..10; A range of number from 10 to 1 : 10 downTo 1 val x = 10 downTo 1
  • 15.
    for loop //// Notlike for loops in languages like C++ or Java //// Used to iterate through a collection Basic Structure : for( item in collection ) { // You could do something with item } Let’s look at few Examples… -> with Ranges -> with List -> break, continue with labels
  • 16.
    Quiz Time!!! //// Whatwill be the output of this? a) fun main() { val x = 1..5 val y = 1..10 for( i in x ){ print("$i : ") for( j in y ){ print("$j ") if( i == 3 && j == 7 ) break } println() } } b)
  • 17.
    while Loop //// Similarto the while loop in C++ //// Used for condition based looping Basic Structure : Let’s look at an Example… while( condition ){ // Execute something }
  • 18.
    do while Loop ////Runs the body of the loop at least ones even if the condition doesn’t satisfies Basic Structure : Let’s look at an Example… do{ // Body of the loop }while ( condition )
  • 19.
    repeat //// Quite notfound in any other languages Basic Structure : repeat(3) { println("Hello") }
  • 22.
  • 23.
    Classes Class is templateof object A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type
  • 25.
  • 26.
    Name : Eshan Age: 20 Gender: Male Occupation : Android Developer Name : Priya Age : 25 Gender : Female Occupation : Doctor
  • 27.
    Classes Class : Car Classis template of object
  • 28.
    To Create aclass , We use class Keyword Properties of Car
  • 29.
    Objects : Instanceof Class Object 1 of Class Car Jaguar I-PACE Black 0 cc Object 2 of Class Car Tata Nano Yellow 624 cc
  • 30.
  • 31.
  • 32.
  • 33.
    There is fasterway of doing this By using a CONSTRUCTOR A constructor is like a special function, and it is defined by using two parantheses () after the class name. You can specify the properties inside of the parantheses (like passing parameters into a regular function). Constructor
  • 34.
    Without Constructor By UsingConstructor Properties Note: Remember to specify the type of the property/variable
  • 35.
    Creating Object byusing Constructor
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
    Inheritance In Kotlin, itis possible to inherit class properties and functions from one class to another SubClass Child SuperClass Parent
  • 41.
  • 42.
    Parent Use the openkeyword in front of the superclass/parent, to make this the class other classes should inherit properties and functions from. Child To inherit from a class, specify the name of the subclass, followed by a colon :, and then the name of the superclass. In Similar fashion we can inherit functions and other properties from parent to child
  • 43.
  • 44.
    Polymorphism Poly means manyand morph means forms i.e. the same method behaves differently based on the object
  • 45.
    we have differentforms of the method that exhibits different behavior based on the object.
  • 46.
    Here we cansee polymorphism of area() Function https://pl.kotl.in/_ebphwnyK
  • 47.
    Abstraction Abstraction is usedto hide background details or any unnecessary implementation about the data so that users only see the required information. It is one of the most important and essential features of object-oriented programming.
  • 48.
    Encapsulation It is aprotective shield that prevents the data from being accessed by the code outside this shield.
  • 49.
    Here we can’taccess private properties of class from outside the class Abstraction Encapsulation Main function don’t know how these setters getters work