fun mainargs ArrayString val e1 Employee val e2 Employee e1allocated

Fun mainargs arraystring val e1 employee val e2

This preview shows page 11 - 14 out of 14 pages.

println("$employee employee is unallocated.") }} fun main(args: Array<String>) { val e1 = Employee() val e2 = Employee() e1.allocated() e2.unallocated() e1.displayAllocationStatus("abc") e2.displayAllocationStatus("xyz") } Output : abc employee is allocated xyz employee is unallocated --------- Try it Out- Classes class KotlinMath { // property (data member) // member functions fun addition(a:Int,b:Int) :Int = a+b fun subtraction(a:Int,b:Int) :Int = a-b fun multiplication(a:Int,b:Int) :Int = a*b fun division(a:Int,b:Int) :Int = a/b } --------- Kotlin Constructors A constructor is a compact way to initialize class properties. In Kotlin, there are two constructors: Primary constructor Secondary constructor Primary Constructor : The primary constructor is part of the class header. class Employee(val firstName: String, var age: Int) { // class body } The block of code surrounded by parentheses is the primary constructor. (val firstName: String, var age: Int) Initializer Blocks A initializer block is used to include the initialization code.
Image of page 11
It is prefixed with init keyword. The code snippet in the previous card is modified with initializer block : fun main(args: Array<String>) { val e1= Employee("Abraham", 25) } class Employee(firstName: String, age: Int) { val fName: String var personAge: Int // initializer block init { fName= firstName personAge= age println("First Name : $fName") println("Age : $personAge") }} When e1 object is created, the code inside initializer block is executed. The initializer block initializes its properties and also prints them. Secondary Constructors A class can contain one or more secondary constructors and are created using constructor keyword Secondary constructors are used to extend a class that provides multiple constructors that initialize the class in different ways Note : Be sure to check Kotlin Inheritance before you learn secondary constructors. An Example : fun main(args: Array<String>) { val p1 = Developer("Sarah") } open class Employee { var data: String = "" var empId = 0 constructor(name: String) { } constructor(name: String, _empId: Int) { data = name empId = _empId println("$data, whose employee id is $empId") }} class Developer: Employee { constructor(name: String): this("Developer details : " + name, 1067) { } constructor(name: String, _empId: Int): super(name, _empId) { }}1. list text here Here, the Employee class has two secondary constructors. Constructors of the derived class Developer calls the corresponding constructor of the base class Employee using super(). One constructor calls another constructor of the Developer class using this(). Inheritance All classes in Kotlin have a common superclass Any, that is a default super class with no supertypes declared. Inheritance allows user to create a new derived class from an existing base class. The derived class inherits all the properties from the base class and can have
Image of page 12
additional properties of its own. open class Employee(age: Int) { //Base Class } class Developer(age: Int): Employee(age) { //Derived Class // other features of Developer } class Tester(age: Int): Employee(age) { // other features of Tester } Here, Employee is a base class, and classes Developer and Tester are derived from the Employee class.
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 14 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture