OBJECT ORIENTED
PROGRAMMING WITH KOTLIN II
Hello and welcome back to
our Kotlin series. In this post we now want to focus on Object oriented
programming with KOTLIN. In this
second part of Object-Oriented Programming with Kotlin, we cover Kotlin inner
and nested classes, Kotlin Data classes, Kotlin sealed classes, Kotlin objects and
companion objects and finally we briefly look at Kotlin extension functions.
Kotlin inner and nested classes
Another fun fact about Kotlin is a file can have more than class in it. We can also nest classes in other classes. You can also nest interfaces. You can nest an interface in a class, a class in an interface and interfaces in interfaces.
Nested classes marked as "inner” are
called inner class. Inner class cannot be
declared inside interfaces or non-inner nested classes. The advantage
of marking a nested class inner is that it is able to access members of
outer class even if it is marked private.
Kotlin Data classes
Data classes are created
solely to hold data. We mark the class as data to create a data class.
To ensure consistency
and meaningful behavior of the generated code, data classes have to fulfill the
following requirements:
· The primary constructor needs to have at least
one parameter;
· All primary constructor parameters need to be
marked as val or var;
· Data classes cannot be abstract, open, sealed
or inner and Data classes may only implement interfaces.
For Data class, the
compiler automatically generates:
· A Copy () function, equals () and hashCode()
pair, and a toString() form of the primary.
· Constructor componentN() functions.
Kotlin Sealed classes
To create a sealed
class, we use sealed modifier. All subclasses of a sealed
class must be declared in the same file where the sealed class is declared. A
sealed class is abstract and therefore you cannot instantiate objects from it. Constructors
of sealed classes are private by default.
Sealed class restricts
the possibility of creating subclasses and when you handle all subclasses of a
sealed class in an when expression, it's not necessary to use else branch.
Kotlin Objects and Companion Objects
In Kotlin we can create
singletons using the object declaration feature. The object
keyword is used. A singleton
is a class that allows one instance of itself to be created and gives access to
that created instance. It is used when a user wants to restrict instantiation of
a class to only one object.
An object declaration can contain properties, methods and so on. They are not allowed to have constructors. Similar to objects of a normal class, you can call methods and access properties by using the (.) notation.
In Kotlin, you can also
call methods by using the class name. For that, you need to create a companion
object by marking object declaration with companion keyword.
If we need a function that can be called without having a class instance but needs access to the internals of a class, we use companion object declaration inside that class. Companion Objects help access the members of the class using the class name only without explicitly creating the instance of the class.
Kotlin Extension Functions.
In Koltin, you can also use extension function to extend a class with new functionality. Basically, an extension function is a member function of a class that is defined outside the class.
Thanks for reading,
till next time keep safe and happy coding.
So, you're a little weird? Work it! A little
different? Own it! Better to be a nerd than one of the herds! -Mandy
Hale

Comments
Post a Comment