lab3
lab3
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
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