Classes and inheritance in Kotlin

Last Updated on December 22, 2020 by AbdurRahman G Official | Md Ghufran Salafi

Classes and inheritance in Kotlin:

 

 

Solution Code by AbdurRahman G Android Developer:

/**
* Program that implements classes for different kinds of dwellings,
* Shows how to:
* Create class hierarchy, variables and functions with inheritance,
* abstract class, overriding, and private vs. public variables.
* */

//Import Statements
import kotlin.math.PI
import kotlin.math.sqrt

//This is the Main Function
fun main() {
val squareCabin = SquareCabin(6, 50.0)
val roundHut = RoundHut(3, 10.0)
val roundTower = RoundTower(4, 15.5)

with(squareCabin) {
println(“\nSquare Cabin \n============”)
println(“Capacity: ${capacity}”)
println(“Material: ${buildingMaterial}”)
println(“Has room? ${hasRoom()}”)
// This Println Statement will print 2 number after decimal point
println(“Floor area: %.2f”.format(floorArea()))
}

with(roundHut) {
println(“\nSquare Cabin \n============”)
println(“Capacity: ${capacity}”)
println(“Material: ${buildingMaterial}”)
println(“Has room? ${hasRoom()}”)
// This Println Statement will print 2 number after decimal point
println(“Floor area: %.2f”.format(floorArea()))
println(“Has room? ${hasRoom()}”)
getRoom()
println(“Has room? ${hasRoom()}”)
getRoom()
// This Println Statement will print 2 number after decimal point
println(“Carpet size: %.2f”.format(calculateMaxCarpetSize()))
}

with(roundTower) {
println(“\nSquare Cabin \n============”)
println(“Capacity: ${capacity}”)
println(“Material: ${buildingMaterial}”)
println(“Has room? ${hasRoom()}”)
// This Println Statement will print 2 number after decimal point
println(“Floor area: %.2f”.format(floorArea()))
// This Println Statement will print 2 number after decimal point
println(“Carpet size: %.2f”.format(calculateMaxCarpetSize()))
}

}

/**
* Defines properties common to all dwellings.
* All dwelling have floorspace,
* but its calculation is specific to the subclass.
* Checking and getting a room are implemented here
* because they are the same for all Dwelling subclasses.
*
* @param residents Current number of residents
* */

abstract class Dwelling (private var residents: Int) {

abstract val buildingMaterial: String
abstract val capacity: Int

/**
* Calculates the floor area of the dwelling.
* Implemented by subclasses where shape is determined.
*
* @return floor area
* */
abstract fun floorArea(): Double

/**
* Checks whether there is room for another resident.
*
* @return true if room available, false otherwise
* */
fun hasRoom(): Boolean {
return residents < capacity
}

/**
* Compare the capacity to the number of residents and
* if capacity is larger than number of residents,
* add residents by increasing the number of residents.
* Print the result.
* */
fun getRoom() {
if (capacity > residents) {
residents++
println(“You got a room”)
} else {
println(“Sorry, at capacity and no rooms left.”)
}
}
}

/**
* A square cabin dwelling.
*
* @param residents Current number of residents
* @param length Length
* */
class SquareCabin(residents: Int, val length: Double) : Dwelling(residents) {

override val buildingMaterial = “Wood”
override val capacity = 6

/**
* Calculates floor area for a square dwelling.
*
* @return floor area
* */
override fun floorArea(): Double {
return length * length
}

}

/**
* Dwelling with a circular floorspace
*
* @param residents Current number of residents
* @param radius Radius
* */
open class RoundHut(
val residents: Int, val radius: Double) : Dwelling(residents) {

override val buildingMaterial = “Straw”
override val capacity = 4

/**
* Calculates floor area for a round dwelling.
*
* @return floor area
* */
override fun floorArea(): Double {
return PI * radius * radius
}

/**
* Calculate the max length for a square carpet
* that fits the circular floor.
*
* @return length of carpet
* */
fun calculateMaxCarpetSize(): Double {
val diameter = 2 * radius
return sqrt(diameter * diameter / 2)
}
}

/**
* Round Tower with multiple stories.
*
* @param residents Current number of residents
* @param radius Radius
* @param floors Number of stories
* */
class RoundTower(
residents: Int,
radius: Double,
val floors: Int = 2) : RoundHut(residents, radius) {

override val buildingMaterial = “Stone”

// Capacity depends on the number of floors.
override val capacity = 4 * floors

/**
* Calculates the total floor area for a tower dwelling
* with multiple stories.
*
* @return floor area
* */
override fun floorArea(): Double {
return super.floorArea() * floors
}
}

Output:

Create class hierarchy, variables and functions with inheritance,  * abstract class, overriding, and private vs. public variables.

 

Solution Code by Android Developer Official Site:

Solution code

This is the complete solution code for this codelab, including comments.

/**
* Program that implements classes for different kinds of dwellings.
* Shows how to:
* Create class hierarchy, variables and functions with inheritance,
* abstract class, overriding, and private vs. public variables.
*/

import kotlin.math.PI
import kotlin.math.sqrt

fun main() {
   val squareCabin = SquareCabin(6, 50.0)
   val roundHut = RoundHut(3, 10.0)
   val roundTower = RoundTower(4, 15.5)

   with(squareCabin) {
       println("\nSquare Cabin\n============")
       println("Capacity: ${capacity}")
       println("Material: ${buildingMaterial}")
       println("Floor area: ${floorArea()}")
   }

   with(roundHut) {
       println("\nRound Hut\n=========")
       println("Material: ${buildingMaterial}")
       println("Capacity: ${capacity}")
       println("Floor area: ${floorArea()}")
       println("Has room? ${hasRoom()}")
       getRoom()
       println("Has room? ${hasRoom()}")
       getRoom()
       println("Carpet size: ${calculateMaxCarpetSize()}")
   }

   with(roundTower) {
       println("\nRound Tower\n==========")
       println("Material: ${buildingMaterial}")
       println("Capacity: ${capacity}")
       println("Floor area: ${floorArea()}")
       println("Carpet size: ${calculateMaxCarpetSize()}")
   }
}


/**
* Defines properties common to all dwellings.
* All dwellings have floorspace,
* but its calculation is specific to the subclass.
* Checking and getting a room are implemented here
* because they are the same for all Dwelling subclasses.
*
* @param residents Current number of residents
*/
abstract class Dwelling(private var residents: Int) {
   abstract val buildingMaterial: String
   abstract val capacity: Int

   /**
    * Calculates the floor area of the dwelling.
    * Implemented by subclasses where shape is determined.
    *
    * @return floor area
    */
   abstract fun floorArea(): Double

   /**
    * Checks whether there is room for another resident.
    *
    * @return true if room available, false otherwise
    */
   fun hasRoom(): Boolean {
       return residents < capacity
   }

   /**
    * Compares the capacity to the number of residents and
    * if capacity is larger than number of residents,
    * add resident by increasing the number of residents.
    * Print the result.
    */
   fun getRoom() {
       if (capacity > residents) {
           residents++
           println("You got a room!")
       } else {
           println("Sorry, at capacity and no rooms left.")
       }
   }

   }

/**
* A square cabin dwelling.
*
*  @param residents Current number of residents
*  @param length Length
*/
class SquareCabin(residents: Int, val length: Double) : Dwelling(residents) {
   override val buildingMaterial = "Wood"
   override val capacity = 6

   /**
    * Calculates floor area for a square dwelling.
    *
    * @return floor area
    */
   override fun floorArea(): Double {
       return length * length
   }

}

/**
* Dwelling with a circular floorspace
*
* @param residents Current number of residents
* @param radius Radius
*/
open class RoundHut(
       val residents: Int, val radius: Double) : Dwelling(residents) {

   override val buildingMaterial = "Straw"
   override val capacity = 4

   /**
    * Calculates floor area for a round dwelling.
    *
    * @return floor area
    */
   override fun floorArea(): Double {
       return PI * radius * radius
   }

   /**
    *  Calculates the max length for a square carpet
    *  that fits the circular floor.
    *
    * @return length of carpet
    */
   fun calculateMaxCarpetSize(): Double {
       val diameter = 2 * radius
       return sqrt(diameter * diameter / 2)
   }

}

/**
* Round tower with multiple stories.
*
* @param residents Current number of residents
* @param radius Radius
* @param floors Number of stories
*/
class RoundTower(
       residents: Int,
       radius: Double,
       val floors: Int = 2) : RoundHut(residents, radius) {

   override val buildingMaterial = "Stone"

   // Capacity depends on the number of floors.
   override val capacity = floors * 4

   /**
    * Calculates the total floor area for a tower dwelling
    * with multiple stories.
    *
    * @return floor area
    */
   override fun floorArea(): Double {
       return super.floorArea() * floors
   }
}

 

Summary

In this codelab you learned how to:

  • Create a class hierarchy, that is a tree of classes where children inherit functionality from parent classes. Properties and functions are inherited by subclasses.
  • Create an abstract class where some functionality is left to be implemented by its subclasses. An abstract class can therefore not be instantiated.
  • Create subclasses of an abstract class.
  • Use override keyword to override properties and functions in subclasses.
  • Use the super keyword to reference functions and properties in the parent class.
  • Make a class open so that it can be subclassed.
  • Make a property private, so it can only be used inside the class.
  • Use the with construct to make multiple calls on the same object instance.
  • Import functionality from the kotlin.math library

Learn more

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *