Advanced Kotlin 1
Previously we would create multiple instances of the class.
Singleton:
Ensures we have only one instance that can access the class
Controls access to resource and avoid conflicts that can happen when accessing class with multiple instances
Suppose we create 2 instances of the RestaurantTables Class.
Instance 1 adds a new customer, but instance 2 list of customers remains unchanged
This is considered as Data Conflict which is a Problem
So using singleton guarantees you have one instance that accesses a class
Singleton can be declared using object declaration. We use object keyword instead of class and
remove parenthesis () because the constructor must private, if you create a constructor, a compiler will give error
Similarly if you try to create an instance of the Object, the compiler will give error
Now this instance cannot be instantiated.
Now we can access the objects properties instantly without creating an object.
In order to call its methods, you can use Object’s qualified name and using . Operator to access the methods.
Like a regular class, singleton object can be inherited by another class or interface
Defining and using companion objects
These are used to store static methods and static data or to provide static methods
Static means data that remains the same and is mutable but is singleton
It is common to have some methods and data common in all instances of the class, this feature is supported in both
Java and C# too.
As we know “Objects” we saw earlier are static, in addition we have companion objects.
In order to achieve the features of the static class, companion objects are static fields or methods declared inside
a class.
We use companion objects if we want some implementation to be a class but still want to implement some behaviors
As static behaviors.
They are initialized when the containing class is resolved.
In kotlin, the Companion object is associated with the class.
A class can have only one Companion Object.
They can be used to implement static members of the class or provide factory methods for creating instances of the
class.
This will be used a lot inside Android development
So make sure you follow coding conventions
These values do not depend on any instance of the class and shared amongst instances of the classes
Functions can be implemented in companion objects which may or may not take values as parameters
but can return values by processing the constant values in the companion objects with parameters passed as input
to the function
For example we can define a function to calculate discount based on the fixed constant rate
Starting another activity and sending arguments to it
Receiving arguments from activity
Extension functions
They can help us extend functionality of a class or an interface we cannot inherit or modify
Through inheritance we can modify certain behaviors of subclasses
A base variant of a car will be use to be extended to become Luxury variant. We want additional features that company
Can provide.
We cannot inherit as car company will not allow us to modify the car during assembling phase.
Here Extension function come in action, and are called as normal functions of the class.
They do not allow you to modify the behavior of the class they are defined for.
They only provide an alternate syntax that allow the functions to be called using . Notation
2. We cannot access private members of the class
3. They are resolved statically (no polymorphism)
4. If class defines member function with same name as extension function the
member function takes precedence
Suppose we want to display Toast messages on the activity screen to the user
Create a new kotlin file and add following
Generic Interface can be created similarly
Limitations of Generics
The type safety is checked at compile time, and at runtime there is no information about the type for e.g
Compile Time Run Time
List<Salad> Only knows list exists but no idea about Salad
List<Beverage> nor Beverages and becomes List<Any?>
• Is called star projection
• Used to represent the case when no info about the type of argument is known, but type safety needs to be
ensured
In order to call Class B from Class A, we need Class B’s reference in Class A
This is called tight coupling.
So with reference to Class B’s instance, Class A has access to all its members, which is
Called overhead since only displayMenuItems() function is required.
We can avoid id by passing this function as an object
Here we see onClick listener but below in the code same is written using lambda expressions which
Makes code easy and short.