Language Elements

Data Types

Kotlin Nullable Types

Exctension Functions

Lambda Functions

Object Oriented Kotlin

Data Classes

Collections

Kotlin Example Codes

Kotlin Interview Questions

  • Kotlin's Class

    Similar to Java a class is a user-defined template with properties and functions to create the objects. A Kotlin class can have a primary constructor and none, one or more secondary constructors. Class is defined by using class key word. An object can be created by using val or var of a class. A typical Kotlin class structure appears as:
    
    
    class KotlinClassName { 
    
       // Properties or data members
       ...
       
       // Functions or Methods
       ...
    }
    
    


  • Kotlin's OOPs Features

    Abstraction, Polymorphism, Encapsulation and Inheritance are supported with the help of classes, objects, and interfaces.

  • Abstraction

    In Kotlin abstract class is declared by declaring abstract key word with class declaration. Such classes cannot be instantiated. They are open by default and can be sub-classed by other non-abstract classes. Abstract methods and fields of these classes are declared with abstract key word. Any non-abstract members of an abstract class are final by default.

  • Encapsulation

    private: Any functions, properties etc are not accessible outside the Kotlin file, where it is defined.

    public: All functions and properties defind inside class can be accessed anywhere. Default Kotlin classes are public.

    protected: Sub classes can access class/interface elements (includes member properties and functions unless they are marked private).

    internal: All elements defined are accessible within the same module.

  • Inheritance

    All the classes in Kotlin are final (non-inheritable). To allow a class to be inherited, it is marked with the open modifier. The child class carries all the methods and properties of parent class, and can also have its own. The extened classes work similarly, as in Java. However, unlike Java, in Kotlin, It is also possible to extend a class, without use of inheritance, by use of extension functions.

  • Polymorphism

    In polymorphism, same method defined by parent class behaves differently based on the object. Kotlin supports compile time and runtime polymorphism. In compile-time polymorphism or overloading, the function name remain same but signatures (parameters) and/or return type is different. For run-time polymorphism, or overriding the call to methods is resolved at runtime by JVM (not by compiler).

  • Functional Programming

    In addition to Object Oriented features, Kotlin also support functional programming. Lambdas (anonymous functions treated as values), higher order function (taking function as paramater or return function) are used to do functional programming with Kotlin.




  • Copyright © by Zafar Yasin. All rights reserved.