Junior — Middle
What is the purpose of inheritance in object-oriented programming?
sobes.tech AI
Answer from AI
The purpose of inheritance in object-oriented programming is to reuse code and create class hierarchies where subclasses inherit properties and methods from parent classes. This allows for creating more specialized objects based on general ones, simplifies maintenance and extension of the code, and helps organize the program into a logical structure.
Example in Kotlin:
open class Animal {
fun eat() { println("Eats") }
}
class Dog : Animal() {
fun bark() { println("Bark") }
}
val dog = Dog()
dog.eat() // call to inherited method