Introduction to Kotlin Object-Oriented Programming

May 22 2024 · Kotlin 1.9.21, Android 14, Kotlin Playground & Android Studio Hedgehog | 2023.1.1

Lesson 03: Inheritance & Polymorphism

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll create a subclass of MuseumObject. You’ll move primaryImageSmall to this subclass, then override the showImage() method.

//  val primaryImageSmall: String,
primaryImageSmall: "https://images.metmuseum.org/CRDImages/ep/original/DT1567.jpg",
fun showImage() {
  return WebViewComposable(url = objectURL) // Updated code
}
class PublicDomainObject(
  objectID: Int,
  title: String,
  objectURL: String,
  creditLine: String,
  isPublicDomain: Boolean,
) : MuseumObject(objectID, title, objectURL, creditLine, isPublicDomain) {

}
open class MuseumObject
val primaryImageSmall: String,
isPublicDomain: Boolean = true,
open fun showImage() {
  //...
}
@SuppressLint("ComposableNaming")
@Composable
override fun showImage() {
  return MuseumObjectComposable(obj = this)
}
fun MuseumObjectComposable(obj: PublicDomainObject) {
  //...
}
val obj_pd =
  PublicDomainObject(
    objectID = 436535,
    title = "Wheat Field with Cypresses",
    objectURL = "https://www.metmuseum.org/art/collection/search/436535",
    primaryImageSmall = "https://images.metmuseum.org/CRDImages/ep/original/DT1567.jpg",
    creditLine = "Purchase, The Annenberg Foundation Gift, 1993",
  )
obj_pd.showImage()
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion