Last active
February 5, 2019 06:45
-
-
Save shrawan2015/4d31d8d28bbdba212f857705ad5791b2 to your computer and use it in GitHub Desktop.
Sealed class in kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Response | |
data class Success(val body: String): Response() | |
data class Error(val code: Int, val message: String): Response() | |
object Timeout: Response() | |
fun sugar(response: Response) = when (response) { | |
is Success -> ... | |
is Error -> ... | |
Timeout -> ... | |
} | |
fun sugar(response: Response) = when (response) { | |
is Success -> println(response.body) | |
is Error -> println("${response.code} ${response.message}") | |
Timeout -> println(response.javaClass.simpleName) | |
} | |
To specify a sealed class, you need to add the modifier sealed. | |
A sealed class cannot be instantiated. Hence, are implicitly abstract. | |
Constructors of a sealed class are private by default. | |
All subclasses of a sealed class must be declared within the same file. | |
sealed class A{ | |
class B : A() | |
{ | |
class E : A() //this works. | |
} | |
class C : A() | |
init { | |
println("sealed class A") | |
} | |
} | |
class D : A() //this works | |
{ | |
class F: A() //This won't work. Since sealed class is defined in another scope. | |
} | |
ealed class A(var name: String){ | |
class B : A("B") | |
class C : A("C") | |
} | |
class D : A("D") | |
fun main(args: Array<String>) { | |
var b = A.B() | |
var d = D() | |
} | |
fun main(args: Array<String>) { | |
val e = A.E("Anupam") | |
println(e) //prints E(name=Anupam) | |
var d = A.D | |
d.name() //prints Object D | |
} | |
sealed class A{ | |
class B : A() | |
class C : A() | |
object D : A() | |
{ | |
fun name() | |
{ | |
println("Object D") | |
} | |
} | |
data class E(var name: String) : A() | |
} | |
sealed class Months { | |
class January(var shortHand: String) : Months() | |
class February(var number: Int) : Months() | |
class March(var shortHand: String, var number: Int) : Msealed class Shape{ | |
class Circle(var radius: Float): Shape() | |
class Square(var length: Int): Shape() | |
class Rectangle(var length: Int, var breadth: Int): Shape() | |
} | |
fun eval(e: Shape) = | |
when (e) { | |
is Shape.Circle -> println("Circle area is ${3.14*e.radius*e.radius}") | |
is Shape.Square -> println("Square area is ${e.length*e.length}") | |
is Shape.Rectangle -> println("Rectagle area is ${e.length*e.breadth}") | |
}onths() | |
} | |
fun main(args: Array<String>) { | |
var circle = Shape.Circle(4.5f) | |
var square = Shape.Square(4) | |
var rectangle = Shape.Rectangle(4,5) | |
eval(circle) | |
eval(square) | |
eval(rectangle) | |
//eval(x) //compile-time error. | |
} | |
sealed class Post | |
{ | |
class Status(var text: String) : Post() | |
class Image(var url: String, var caption: String) : Post() | |
class Video(var url: String, var timeDuration: Int, var encoding: String): Post() | |
} | |
sealed class Shape{ | |
class Circle(var radius: Float): Shape() | |
class Square(var length: Int): Shape() | |
class Rectangle(var length: Int, var breadth: Int): Shape() | |
} | |
fun eval(e: Shape) = | |
when (e) { | |
is Shape.Circle -> println("Circle area is ${3.14*e.radius*e.radius}") | |
is Shape.Square -> println("Square area is ${e.length*e.length}") | |
is Shape.Rectangle -> println("Rectagle area is ${e.length*e.breadth}") | |
} | |
Note: The is modifier checks if the class is of the following type. | |
is modifier is required only for classes. Not with Kotlin objects as shown below: | |
sealed class Shape{ | |
class Circle(var radius: Float): Shape() | |
class Square(var length: Int): Shape() | |
object Rectangle: Shape() | |
{ | |
var length: Int = 0 | |
var breadth : Int = 0 | |
} | |
} | |
fun eval(e: Shape) = | |
when (e) { | |
is Shape.Circle -> println("Circle area is ${3.14*e.radius*e.radius}") | |
is Shape.Square -> println("Square area is ${e.length*e.length}") | |
Shape.Rectangle -> println("Rectangle area is ${Shape.Rectangle.length*Shape.Rectangle.breadth}") | |
} | |
//Companion object is used as static : | |
class Person { | |
companion object Test { | |
fun callMe() = println("I'm called.") | |
} | |
} | |
fun main(args: Array<String>) { | |
Person.callMe() | |
} | |
The name of the companion object is optional and can be omitted. | |
class Person { | |
// name of the companion object is omitted | |
companion object { | |
fun callMe() = println("I'm called.") | |
} | |
} | |
fun main(args: Array<String>) { | |
Person.callMe() | |
} | |
//Difference in object and companion // difference in the static and singleton class | |
class MyClass { | |
object Holder { | |
//something | |
} | |
companion object { | |
//something | |
} | |
} | |
//One advantage that companion objects have over static members is that they can inherit from other classes or implement interfaces and generally behave like any other singleton. | |
However, unless they need access to the private members of a class, the truth is that there’s really no need for static members at all when you can declare ‘top level’ properties and functions in Kotlin. | |
class MyClass { | |
companion fun foo() { | |
} | |
companion fun bar() { | |
} | |
} | |
is equivalent to | |
class MyClass { | |
companion object { | |
fun foo() { | |
} | |
fun bar() { | |
} | |
} | |
} | |
abstract class DefaultColored(var defaultColor: Long) | |
abstract class Shape { | |
abstract fun paint() : Unit | |
companion object : DefaultColored(1) | |
} | |
class Text : Shape() { | |
override fun paint() { | |
println("text: $defaultColor") | |
} | |
companion object : DefaultColored(2) | |
} | |
class Box: Shape() { | |
override fun paint() { | |
println("box: $defaultColor") | |
} | |
} | |
fun main(args: Array<String>) { | |
Text().paint() | |
Box().paint() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment