Having gone through the concepts of classes in Kotlin, we understand that classes form the basis for any OOP’s oriented language and Kotlin is no different. As evident from the name itself, Sealed Classes are the classes which are closed or sealed, which makes their usage restricted.
In this tutorial we will discuss about Kotlin exception handling concept in detail.
Errors, Exceptions are the developer’s nightmare as well as help guide. Let’s understand the basic difference between Error and Bugs.
Anything which breaks the normal flow of control of a program can be termed as an Exception.
As a developer, we always have faced NullPointerException, or commonly known as ‘The Billion Dollar Mistake’. Every introduction of kotlin talks about the kotlin’s capablity to handle Null Pointers. Yes, take it as a relief Kotlin can handle null pointers for you, thanks to its type system.
Talking about interface, there are two types of implementations which exists, the pre Java 8 ones where interfaces could contain only abstract methods. But in the recent update of Java 8, where interfaces were allowed to have implementations of methods as well as abstract methods. Kotlin offers similar type of interfaces i.e. Interface in kotlin are allowed to have implemented methods as well as abstract methods.
A class which is declared with keyword ‘abstract’ is termed as Abstract Class, these can also be termed as classes which can’t be instantiated. Or in Simple words, we cannot create objects of Abstract classes. Similar to Java, abstract classes in Kotlin can be inherited in sub classes.
Before we jump on and discuss about Kotlin Getters and Setters it is important for us to understand the basic working of Properties in Kotlin.
Having read about the kotlin classes and objects, let’s have a look at initialization of class objects. This can be done using constructors. So in simple words:
“A constructor is a special member function which is called when the object is instantiated or created.”
Having read about classes and objects in detail, we now can recall of scenarios in our applications where we create certain classes which act as place holder for the data. Along with data these classes contain basic standard functions and utilities. The common functions these class hold are the getters, the setters, the toString method, equals and hashcode.
Before we move ahead with the basic user defined data structure of classes and the ways to use them by the means of Object, let’s have a look at the fundamentals which go inside Kotlin.