Language Elements

Data Types

Exctension Functions

Object Oriented Kotlin

Data Classes

Coroutines

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 appear as:
    
    
    class KotlinClassName { 
    
       // Properties or data members
       ...
       
       // Functions or Methods
       ...
    }
    
    


  • Kotlin's OOPs features

    Abstraction, polymorphism, encapsulation, inheritance are supported with the help of classes, objects, and interfaces.

  • Abstraction

    In Kotlin abstract class is declared by declaring abstract key word in front of the class. Such classes cannot be instantiated. It is open by default and can be sub-classed by other non-abstract classes. Abstract methods and fields of abstract class 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 by others, it is marked with the open modifier.

  • 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, the function signatures remains the same but parameters or return type is different. At compile time, function called is resolved based on the type of parameters. For run-time polymorphism, the compiler resolves a call to overridden/overloaded methods at runtime.

  • Functional Programming features: Lambdas (anonymous functions treated as values), higher order function (taking function as paramater or return function) etc are used to do functional programming with Kotlin.




  • Copyright © by Zafar Yasin. All rights reserved.