Inheritance is one of the key concepts in object-oriented programming. It involves avoiding code-repetition, especially where different classes have common features and all the classes belong to the same type. Let's say we are writing software for an educational institute in which three different entities are involved—Student, Professor, and Employee:
All these entities have some properties in common, including name, age, and id. Each entity exhibits some behaviors, such as speak or greet. Let's convert the diagram of the Professor class into code:
class Professor(val fName: String, var lName: String, var pAge: Int, val professorId : String ) {
fun speak() {
println("My name is $fName $lName age is $pAge and my
ID is $professorId")
}
fun greet() {
println("Hi there... Professor $fName...