In Kotlin both the header and the body are optional; if the class has no body, curly braces can be omitted.
So we can define class like,
class Empty
What is the use of this type of class?
A Kotlin Data Class is used to hold the data only and it does not provide any other functionality apart from holding data. There are following conditions for a Kotlin class to be defined as a Data Class: The primary constructor needs to have at least one parameter.
Classes provide blueprints from which objects can be constructed. An object is an instance of a class that consists of data specific to that object. You can use objects or class instances interchangeably.
Kotlin interfaces are similar to abstract classes. However, interfaces cannot store state whereas abstract classes can. Meaning, interface may have property but it needs to be abstract or has to provide accessor implementations. Whereas, it's not mandatory for property of an abstract class to be abstract.
Kotlin class A class is a blueprint for an object; it shares common properties and behaviour in form of members and member functions. In Kotlin, a class is declared with the class keyword. The class declaration consists of the class name, the class header (specifying its type parameters, the primary constructor etc.)
You can use it for some custom exceptions:
class Empty : Exception()
or as a marker interface:
interface Empty
or as a data class:
data class Empty(val s: String)
or as a marker annotation:
annotation class Empty
~ That's a good post to read.
Kotlin allows to declare any type without body, for example:
interface Interface;
class Class;
annotation class Annotation;
sealed class SealedClass;
data class DataClass(var value: String);
object ObjectClass;
enum class EnumClass;
class CompanionClass {
companion object
}
the usage of each definition can be described as below:
@Before
and @After
annotations.synchronized(lock){ /*thread safe working*/ }
getters
, setters
? , equals
, hashCode
, toString
and componentN
operators for destructuring in kotlin.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With