Dive into Object-Oriented Programming with Kotlin


When studying to jot down Kotlin for the primary time, you aren’t simply studying find out how to string collectively advanced chains of seemingly arcane symbols, you’re truly studying find out how to symbolize issues in a method for the pc to know. But, individuals want to know the code as nicely. But, what’s “good” code?

All through the years, sure patterns and methods have advanced within the developer neighborhood. A few of these ideas have been included instantly right into a language whereas different methods and finest practices are used along side these language options. Because of this, understanding find out how to construction and write your code is simply as necessary as studying the syntax and key phrases.

Within the following excerpt, Emmanuel Okiche covers the ideas of summary courses and interfaces in Kotlin. You’ll find out how and why to make use of these language constructs in your personal code. Within the course of, you’ll acquire a preview of Kodeco’s Object-Oriented Programming with Kotlin course.

Summary Lessons

Generally, you could need to stop a category from being instantiated however nonetheless be capable to be inherited from. This can allow you to outline properties and habits widespread to all subclasses. Such a father or mother class is known as an summary class. These courses can’t be instantiated, which means you possibly can’t create an object of an summary class. You’ll be able to consider these courses as templates for different courses: simply base model, configurations, and performance tips for a particular design. The template can’t run instantly in your app. As a substitute, your app could make use of the template.

Lessons declared with the summary key phrase are open by default and could be inherited from. In summary courses, you can even declare summary strategies marked with summary that don’t have any physique. The summary strategies should be overridden in subclasses. Because the predominant cause for summary courses is for different courses to increase them, they will’t be non-public or ultimate. Although, their strategies and properties are ultimate by default, until you make them summary, which makes them open for overriding.

Check out this:


summary class Animal {
  summary val identify: String // Summary Property
}

summary class Mammal(val birthDate: String): Animal() { // Non-Summary Property (birthDate)
  summary enjoyable consumeFood() // Summary Methodology

  summary val furColor: Record // Summary Property

  // Non-Summary Methodology
  enjoyable someMammalMethod() {
    println("Non summary operate")
  }
}

class Human(birthDate: String): Mammal(birthDate) {
  // Summary Property (Have to be overridden by Subclasses)
  override val identify = "Human"

  // Summary Property (Have to be overridden by Subclasses)
  override val furColor = listOf("brown", "black")

  // Summary Methodology (Have to be applied by Subclasses)
  override enjoyable consumeFood() {
    // ...
  }

  // Member methodology created by this class (Not Inherited)
  enjoyable createBirthCertificate() {
    // ...
  }
}

Right here, you may have Animal and Mammal courses, that are each summary, and the Mammal class inherits from Animal. We even have the Human class which inherits from Mammal.

It would appear like loads is occurring within the code above, nevertheless it’s easier than you suppose. Right here’s the breakdown:

  1. The Animal class is an summary class that has one summary property; identify. Which means the subclasses should override it.
  2. Subsequent, you may have the Mammal summary class that extends the Animal class, which implies that Mammal is-a Animal.
    • It has a combination of each summary and non-abstract members. Summary courses can have non-abstract members.
    • The identify property from the Animal father or mother class isn’t overridden right here. However that’s okay—Mammal is an summary class too, so it simply implies that identify should be applied someplace down the road within the inheritance tree. In any other case, you’ll get an error.
  3. The Human class extends the Mammal class, which implies that Human is-a Mammal.
    • It overrides the identify property from the Animal class, which was handed down by Mammal.
    • It additionally overrides Mammal summary members and creates its personal createBirthCertificate() methodology.

Now, see what occurs if you attempt to create an occasion of every of those:


val human = Human("1/1/2000")
val mammal = Mammal("1/1/2000") // Error: Can't create an occasion of an summary class

Bear in mind, summary courses can’t be instantiated, and that’s why attempting to instantiate Mammal causes an error.

Now, summary courses are cool, however Kotlin doesn’t assist a number of inheritance. Which means a category can solely lengthen one father or mother class. So, a category can solely have one is-a relationship. This could be a bit limiting relying on what you need to obtain. This leads us to the following assemble, “Interfaces.”

Utilizing Interfaces

To this point, you’ve been working with the customized sort, Class. You’ve discovered about inheritance and the way a category can lengthen an summary and non-abstract class which are associated. One other very helpful customized sort is Interfaces.

Interfaces merely create a contract that different courses can implement. Bear in mind, you imagined summary courses as web site or cell templates above, and this implies we will’t use a couple of template for the app on the similar time. Interfaces could be seen as plugins or add-ons which add a function or habits to the app. An app can have just one template however can have a number of plugins linked to it.

A category can implement a number of interfaces, however the courses that implement them should not be associated. You might say that interfaces exhibit the is relationship relatively than the is-a relationship. One other factor to notice is that almost all interfaces are named as adjectives, though this isn’t a rule. For instance, Pluggable, Comparable, Drivable. So you may say a Tv class is Pluggable or a Automobile class is Drivable. Bear in mind, a category can implement a number of interfaces, so the Automobile class could be Drivable and on the similar time Chargeable if it’s an electrical automotive. Similar factor with a Telephone is Chargeable regardless that Automobile and Telephone are unrelated.

Now, think about you may have two courses Microwave and WashingMachine. These are totally different electrical home equipment, however they’ve one factor in widespread, they each must be linked to electrical energy to operate. Units that connect with electrical energy at all times have some necessary issues in widespread. Let’s push these commonalities to an interface.

Check out how you may do that:


interface Pluggable {

  // properties in interfaces can not keep state
  val neededWattToWork: Int 
  
  // this would possibly not work. would end in an error due to the explanation above
  // val neededWattToWork: Int = 40 

  //Measured in Watt
  enjoyable electricityConsumed(wattLimit: Int) : Int

  enjoyable turnOff()

  enjoyable turnOn()
}

class Microwave : Pluggable {

  override val neededWattToWork = 15

  override enjoyable electricityConsumed(wattLimit: Int): Int {
    return if (neededWattToWork > wattLimit) {
      turnOff()
      0
    } else {
      turnOn()
      neededWattToWork
    }
  }

  override enjoyable turnOff() {
    println("Microwave Turning off...")
  }

  override enjoyable turnOn() {
    println("Microwave Turning on...")
  }
}

class WashingMachine : Pluggable {

  override val neededWattToWork = 60

  override enjoyable electricityConsumed(wattLimit: Int): Int {
    return if (neededWattToWork > wattLimit) {
      turnOff()
      0
    } else {
      turnOn()
      neededWattToWork
    }
  }

  override enjoyable turnOff() {
    println("WashingMachine Turning off...")
  }

  override enjoyable turnOn() {
    println("WashingMachine Turning on...")
  }
}

You’ll be able to see that the Pluggable interface creates a contract that each one courses implementing it should comply with. The members of the interface are summary by default, in order that they should be overridden by subclasses.

Notice: Properties in interfaces can’t keep their state, so initializing it could end in an error.

Additionally, interfaces can have default methodology implementation. So turnOn might have a physique like so:


enjoyable turnOn() {
  println("Turning on...")
}

Let’s say the WashingMachine subclass doesn’t override it. Then you may have one thing like this:


val washingMachine = WashingMachine()
washingMachine.turnOn() // Turning on...

The output will probably be “Turning on…” as a result of it was not overridden within the WashingMachine class.

When an interface defines a default implementation, you possibly can nonetheless override the implementation in a category that implements the interface.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles