Complete the code to declare a class that can be inherited.
open class Animal { fun sound() { println("Animal sound") } } class Dog : [1]() { }
The class Dog inherits from Animal. The open keyword on Animal allows inheritance.
Complete the code to allow the function to be overridden in a subclass.
open class Vehicle { open fun drive() { println("Driving vehicle") } } class Car : Vehicle() { override fun [1]() { println("Driving car") } }
The function drive is overridden in the subclass Car. The open keyword on the base function allows this.
Fix the error by adding the required keyword to allow inheritance.
class Person { fun greet() { println("Hello") } } class Student : [1]() { }
The class Person must be marked open to allow inheritance. Since it is not, inheritance causes an error.
Fill both blanks to create an open class and override its function.
class [1] { fun greet() { println("Hi") } } class Friend : [2]() { override fun greet() { println("Hello friend") } }
The base class Person must be declared open to allow inheritance and function overriding. The subclass Friend inherits from Person.
Fill all three blanks to define an open class, an open function, and override it in a subclass.
class [1] { [2] fun speak() { println("Speaking") } } class Teacher : [3]() { override fun speak() { println("Teaching") } }
The base class Person is declared normally. The function speak is marked open to allow overriding. The subclass Teacher inherits from Person which must be marked open to allow inheritance.