0% found this document useful (0 votes)
10 views31 pages

lab3

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

lab3

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

MOBILE PROGRAMMING Prepared by:

Eng. Marwa ELDeeb


I
KOTLIN ARRAYS
Arrays are used to store multiple values in a single variable,
instead of creating separate variables for each value.
To create an array, use the arrayOf() function, and place the values
in a comma-separated list inside it.
val names = arrayOf(“ahmed" , “mohamed" , “mahmoud" ,
“tarek")
KOTLIN ARRAYS
fun main(args: Array<String>) {
val names = arrayOf("ahmed" , "mohamed" , "mahmoud" , "tarek")
println(names[0]) // Access the Elements of an Array
names[1] = "tarek" //Change an Array Element
println(names[1])
}

Output:
ahmed
tarek
KOTLIN ARRAYS
To find out how many elements an array have, use the size property.
fun main(args: Array<String>) {
val names = arrayOf("ahmed" , "mohamed" , "mahmoud" ,
"tarek") //4
println(names.size)
You can use the in operator to check if an element exists in an array.
KOTLIN ARRAYS
In Kotlin, the for loop is used to loop through arrays with the in
operator.
KOTLIN ARRAYS
With the for loop, you can also create ranges of values with "..“
KOTLIN ARRAYS
What is the output?
KOTLIN ARRAYS
What is the output?
KOTLIN FUNCTION
To create your own function, use the fun keyword, and write the
name of the function, followed by parentheses ().
KOTLIN ”OOP”
OOP stands for Object-Oriented Programming.
Object-oriented programming is about creating objects that contain
both data and methods.
A class is a template for objects, and an object is an instance of a
class.
To create a class, use the class keyword, and specify the name of the
class.
The object can access properties and functions of a class by using (.)
notation.
KOTLIN ”OOP”
class Car {
var brand = ""
var model = ""
var year = 0}
fun main(){
val c1 = Car()
c1.brand = "Ford"
c1.model = "Mustang"
c1.year = 1969
println(c1.brand) // Outputs Ford
println(c1.model) // Outputs Mustang
println(c1.year) } // Outputs 1969 }
KOTLIN ”OOP”
A constructor is a concise way to initialize class properties. It is a
special member function that is called when an object is created.
In Kotlin, there are two constructors:
Primary constructor - concise way to initialize a class
Secondary constructor - allows you to put additional initialization logic
PRIMARY CONSTRUCTOR
PRIMARY CONSTRUCTOR
To put the initilization code, initializer block is used. It is prefixed with
init keyword.
DEFAULT VALUE IN PRIMARY
CONSTRUCTOR
SECONDARY CONSTRUCTOR
INHERITANCE
In Kotlin, it is possible to inherit class properties and functions from
one class to another. We group the "inheritance concept" into two
categories:
 subclass (child) - the class that inherits from another class .
 superclass (parent) - the class being inherited from.
Use the open keyword in front of the superclass/parent, to make this
the class other classes should inherit properties and functions from.
To inherit from a class, specify the name of the subclass, followed by
a colon :, and then the name of the superclass
INHERITANCE
class MyChildClass: MyParentClass() {
fun myFunction() {
println(x) // x is now inherited from the superclass
}
}
// Create an object of MyChildClass and call myFunction
fun main() {
val myObj = MyChildClass()
myObj.myFunction()
}
// output will be 5
INHERITANCE AND
OVERRIDING
In inheritance, we can provide specific implementation of the base
class functions inside the derived class. It means if a function exists in
parent class then we can provide a different implementation of the
same function in child class. It is known as Function Overriding.
To override a function, it must be marked as open (just like the
class) in the parent class and in the child class we must use the
keyword override to specify that we have overridden the function.
INHERITANCE AND
OVERRIDING
open class MyParentClass {
val x = 5}
open class Person{
open fun printMessage(){
println("Message for Person") } }
class Child: Person() {
override fun printMessage(){
println("Message for Child") } }
open class Boy : Person(){
final override fun printMessage(){
println("Message for Boys") } }
class Hero : Boy() {
fun printData(){
super.printMessage()
println("Hello Hero") } }
fun main(args:Array<String>){
val person=Person()
person.printMessage()
val child=Child()
child.printMessage()
val hero=Hero()
hero.printData() }
VISIBILITY MODIFIERS
There are four visibility modifiers in Kotlin:
 public visible everywhere.
 private visible (can be accessed) from inside the class only.
 protected visible to the class and its subclass.
 internal any client inside the module can access them.” module is a
set of Kotlin files compiled together”
The default visibility is public.
If you override a protected or an internal member and do not
specify the visibility explicitly, the overriding member will also have
the same visibility as the original.
VISIBILITY MODIFIERS
open class Outer {
private val a = 1
protected open val b = 2
internal open val c = 3
val d = 4 // public by default

protected class Nested {


public val e: Int = 5
}
}
class Subclass : Outer() {
// a is not visible
// b, c and d are visible
// Nested and e are visible

override val b = 5 // 'b' is protected


override val c = 7 // 'c' is internal
}
class Unrelated(o: Outer) {
// o.a, o.b are not visible
// o.c and o.d are visible (same module)
// Outer.Nested is not visible, and Nested::e is not visible either
PACKAGE
Packages are used to group related classes.
Packages are declared with the package keyword, and any Kotlin file
with a package declaration at the beginning can contain declarations
of classes, functions, or interfaces
ABCTRACT CLASS
An abstract class cannot be instantiated (you cannot create objects
of an abstract class). However, you can inherit subclasses from can
them.
abstract keyword is used to declare abstract classes in Kotlin.
The members (properties and methods) of an abstract class are non-
abstract unless you explicitly use abstract keyword to make them
abstract.
ABCTRACT CLASS
 An abstract class Person is created. the class has a non-abstract
property age and a non-abstract method displayIDnumber(). The
class has an abstract method displayJob(). It doesn't have any
implementation and must be overridden in its subclasses.
 Note: Abstract classes are always open. You do not need to
explicitly use open keyword to inherit subclasses from them.
ABCTRACT CLASS
abstract class Person(name: String) {
init {
println("My name is $name.") }
fun displaySSN(ssn: Int) {
println("My SSN is $ssn.")
}
abstract fun displayJob(description: String)
}
class Teacher(name: String): Person(name) {
override fun displayJob(description: String) {
println(description) OUTPUT
}}
fun main(args: Array<String>) {
val jack = Teacher("Jack Smith")
jack.displayJob("I'm a mathematics teacher.")
jack.displaySSN(23123) }
INTERFACE
Kotlin interfaces can contain definitions of abstract
methods as well as implementations of non-abstract
methods.
Abstract classes in Kotlin are similar to interface with one
important difference. It's not mandatory for properties of an
abstract class to be abstract or provide accessor
implementations.
Keyword interface is used to define interfaces in Kotlin
INTERFACE
Kotlin interfaces can contain definitions of abstract
methods as well as implementations of non-abstract
methods.
Abstract classes in Kotlin are similar to interface with one
important difference. It's not mandatory for properties of an
abstract class to be abstract or provide accessor
implementations.
Keyword interface is used to define interfaces in Kotlin
INTERFACE
interface MyInterface {
val test: Int
fun foo() : String

fun hello() {
println("Hello there, pal!")
}
}
class InterfaceImp : MyInterface {
override val test: Int = 25
override fun foo() = "Lol"
}
fun main(args: Array<String>) {
val obj = InterfaceImp()
println("test = ${obj.test}")
print("Calling hello(): ")
obj.hello()
print("Calling and printing foo(): ")
OUTPUT
println(obj.foo())
}
INTERFACE
Kotlin does not allow true multiple inheritance. However, it's
possible to implement two or more interfaces in a single class. For
example:

OUTPUT
THANKS Eng.Marwa
ELDeeb

You might also like