What do we call a programming language that has classes and objects?
Object Oriented Programming (OOP)
What keyword is used to change a function or properties that are already existed in the parent class, in the child class?
The keyword "override" is used to change the function or properties that are already existed in the parent class, in the child class.
What is the keyword used to differentiate an abstract class from a normal class?
"abstract" is the keyword used to mark a class as an abstract class.
A class with no constructor declared means that it does not have any constructor at all, true or false?
False, if the constructor of a class is not declared, Kotlin will generate a default constructor.
How many parent classes can a child class inherits?
1
How many parent abstract classes can a child class inherits?
1
What is the difference between a constructor and a function?
A constructor is a special function that is called when an object is instantiated. It does not return any value whereas a function returns a value.
Can a parent class accesses the properties of its child classes?
No
Do we need open keyword for overriding an abstract class?
No, because it is an abstract class.
All properties of a class allow getters and setters, true or false?
False, properties of a class declared with val keyword do not allow setters because val is immutable.
open class A {
fun sayHi() {
println("Hi")
}
}
class B: A() {
fun sayBye() {
println("Bye")
}
}
fun main() {
val b:A = B()
b.sayHi()
b.sayBye()
}What are the outputs from the code above?
Error, because variable b's type is A and class A does not have sayBye() function.
What are the differences between abstract property/function and a normal property/function?
An abstract property/function doesn't have the body or implementation whereas a normal property/function needs to have function body or implementation.
class Person {}
fun main() {
val x = Person()
val y = Person()
println(x == y)
}What is the output of the code above?
False, Person object in x is different the Person object in y, because they are different instances.
open class A {
fun sayHi() {
println("Hello from A")
}
}
class B: A() {
override fun sayHi() {
println("Hello from B")
}
}For B to override the sayHi() function from A, what is missing from the code above?
In order for class B to override the function that is already existed in class A, the keyword "open" is needed to be declared in front of the sayHi() function in class A.
abstract class A {
abstract var a: String
abstract fun sayHi()
}
class B: A() {
override var a: String = "Hi"
override fun sayHi() {
println(a)
}
}Will the overrides in class B work?
Yes, it will work.