Sunday, May 3, 2020

Kotlin - Modifiers , Enum classes ,Sealed classes


Visibility Modifiers 

It lets you set the visibility of any code that you create such as classes and functions. For example, a member function can only be used inside its class.

So Kotlin has four visibility modifiers : 

1) public
2)private
3)protected
4)internal

Modifiers:  What it does:

public        It makes the declaration visible everywhere.This is applied by default so it can be omitted.

private       It makes the declaration visible to code inside its source file but invisible elsewhere.

internal      It makes the declaration visible inside the same module but invisible elsewhere.

Lets take an example :

open class Parent
{
  var a = 1

   private var b =2

   protected open var c = 3

   internal var d =4
 
}         

class Child : Parent()
{
  override var c = 6
}

Note that if you override a protected member as in the above code, the subclass version of that member will also be protected by default.you can however
change its visibility as in this example,

class Child : Parent()
{
 public override var c = 6
}

Enum classes :

An enum class lets you create a set of values that represent the only valid values for a variable.

for example:

enum class BandMember { AVI,KUNAL,PRASHANT} // the enum class has 3 values.

fun main(args : Arrays<String>)
{
   var selectedBandMember : BandMember // The variable's type is BandMember
   selectedBandMember = BandMember.AVI  // So we can assign one of BandMembers value to it.
}

Note : 


  • Each value in an enum class is constant.
  • Each enum constant exists as a single instance of that enum class.
enum properties and functions example:


enum class BandMember(val instrument : String) { AVI("Lead Guitar"),KUNAL("Rythm guitar"),PRASHANT("bass");

fun sings() = "occasionally"  // Each enum value has a function named sings which return the String
}

enum class BandMember(val instrument : String)
{
  AVI("Lead Guitar")
  {
    override fun sings() = "plaintively"
  },
  KUNAL("Rythm Gutar")
  {
    override fun signs() = "hoarsely"
  },
  PRASHANT("Bass")
  {
  };

  open fun signs() = "occasionally"

}

fun main(args : Array<String>)
{
   var selectedBandMember : BandMember

   selectedBandMember = BandMember.AVI
 
   println(selectedBandMember.instrument)
 
   println(selectedBandMember.sings())
}

Sealed classes 

Suppose that you want to be able to use two different message types in your application: one for success and another for failure.

How to do this?

enum class Message Type(var msg: String)
{
  SUCCESS("YAH!!"),
  FAILURE("Failed!!")
}

But there is a problem with this approach:

1) Each value is a constant which only exists as a single instance.
2)Each value must have the same properties and functions.

So what's the solution?

Sealed classes to the rescue!:

So a sealed class is like a souped-up version of an enum class.it lets restrict your class hierarchy to a specific set of subtypes each one of which can be defined as its own properties and functions.

You can create a sealed class by prefixing the class name with sealed.

for example;

 sealed class Message Type
 class MessageSuccess(var msg: String) : MessageType()
 class MessageFailure(var msg: String) : MessageType()

fun main(args : Array<String>)
{
   var messageSucc : MessageSuccess ("Successful")
   var messageSucc2 : MessageSuccess ("It worked Successful")
   var messageFail : MessageSuccess ("Failed")
}





No comments: