You are on page 1of 8

Mobile Programming

(Kotlin Language)

Lab 4

Prepared By:

Eng.Marwa ELDeeb

Contact info:mmamdeeb@gmail.com

|Page1
Quick Recap

Important notes:

 There are four visibility modifiers in Kotlin:


1. public visible everywhere.
2. private visible (can be accessed) from inside the class only.
3. protected visible to the class and its subclass.
4. 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.

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}

 Packages are used to group related classes.


 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.

|Page2
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)
}
}

fun main(args: Array<String>) {


val jack = Teacher("Jack Smith")
jack.displayJob("I'm a mathematics teacher.")
jack.displaySSN(23123)
}

Output:

My name is Jack Smith.


I'm a mathematics teacher.
My SSN is 23123.

 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.

|Page3
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(): ")


println(obj.foo())
}

Output:

test = 25
Calling hello(): Hello there, pal!
Calling and printing foo(): Lol

 Kotlin does not allow true multiple inheritance. However, it's possible to
implement two or more interfaces in a single class. For example:

|Page4
interface A {

fun callMe() {
println("From interface A") }
}

interface B {
fun callMeToo() {
println("From interface B")
}
}

// implements two interfaces A and B


class Child: A, B

fun main(args: Array<String>) {


val obj = Child()

obj.callMe()
obj.callMeToo()
}

Output:

From interface A
From interface B

 Kotlin allows you to define a class within another class known as nested
class.
 you can use (.) notation to access Nested class and its members.
 The nested classes in Kotlin do not have access to the outer class
instance.
 you need to use inner modifier to create an inner class which we will
discuss next.
 An exception is an unwanted or unexpected event, which occurs during
the execution of a program.
 In Kotlin, we use try-catch block for exception handling in the program.
The try block encloses the code which is responsible for throwing an
exception and the catch block is used for handling the exception.
 finally block is always executes irrespective of whether an exception is
handled or not by the catch block. So it is used to execute important code
statement.

|Page5
 we use throw keyword to throw an explicit exception. It can also be used
to throw a custom exception.

Please answer the following


1. what is the output?
private class A {
private val int = 10
fun display()
{
// we can access int in the same class
println(int)
println("Accessing int successful")
}
}
fun main(args: Array<String>){
var a = A()
a.display()
// can not access 'int': it is private in class A
println(a.int)
}
2. what is the output?
open class A {
// protected variable
protected val int = 10
}

// derived class
class B: A() {
fun getvalue(): Int {
// accessed from the subclass
return int
}
}

fun main(args: Array<String>) {


var a = B()
println("The value of integer is: "+a.getvalue())
}

|Page6
3. what is the output?
// base class
open class A {
// protected variable
open protected val int = 10

// derived class
class B: A() {
override val int = 20
fun getvalue():Int {
// accessed from the subclass
return int
}
}

fun main(args: Array<String>) {


var a = B()
println(a.getvalue())
}

fun main(args: Array<String>) {


val eng = Engineer("Praveen",2)
eng.employeeDetails()
eng.dateOfBirth("02 December 1994")
}
4. use the following information to run the above main
create abstract class called employee with primary constructor that take
(name, experience), the employee has abstract property called salary,
abstract method called date_of_birth(date) and finally non abstract method
called employee_details() that allow us to know the (name,year of experience,
annual salary) of the employee.

Create class Engineer(name: String,experience: Int) that inherete from


employee and decide the main properties and methods that must found on
it.
The output must be:
Name of the employee: Praveen
Experience in years: 2
Annual Salary: 500000.0
Date of Birth is: 02 December 1994

|Page7
5. what is the output?

class Animal{
// outer class property
val legs = 4
class Dog{
// nested class proprty
val leg=legs
val say = "Woof"
val hasTail = true
fun tail(){
println("Dog has tail: $hasTail")
}
}
}

fun main() {
// Creating object of nested class directly
val dog = Animal.Dog()
// Accessing inner class
dog.tail()
println("Dog says: ${dog.say}")
}

6. what is the output?


fun main(args: Array<String>) {
try {
val myVar:Int = 12;
val v:String = "Tutorialspoint.com";
v.toInt();
} catch(e:Exception) {
e.printStackTrace();
} finally {
println("Exception Handeling in Kotlin");
}
}

|Page8

You might also like