By default, Kotlin classes are final – they can't be inherited. To make a class inheritable, mark it with the open keyword:
All classes in Kotlin have a common superclass, Any, which is the default superclass for a class with no supertypes declared:
Like java we can not just extend the class. By default all class are final in kotlin.
By default, Kotlin classes are final: they can't be inherited. To make a class inheritable, it has to be marked with the open keyword.
Parent constructor & init block called first
Ambiguity in parent child class. it suggest for the keyword ovverride
final methods can not be ovverriden. so make it open
Because of this experience in Java, the Kotlin designers decided to make all classes and methods final by default. That way, you can “design for inheritance” by marking classes and functions as open only when you really want them to be open to extension.
Every kotlin class has Any as a superclass
Any class has following methods
open operator fun equals(other: Any?): Boolean
open fun hashCode(): Int
open fun toString(): String
All methods are open so child class can override it
whenever we write tostring method in our class it shows override suggestion. because Any is the parent and it has tostring method
Inheritance & Overriding Videos
Polymorphism is achieved by inheritance. Abstraction is not mandatory to achieve polymorphism
To execute a common command in all the objects we need a array which has same data type for all the diff objects so to do so we make a parent class and use its name as a common data type for all child objects
so that in for loop we can fire the same command to all the objects
using abstraction we can make sure that all the child object implement same method which we call from the for loop
every child object behaves differently according to their implementation in common implemented method
Abstract class is incomplete so we can not create an object of it
Abstract methods are by default open
Abstract classes are always open. You do not need to explicitly use open keyword to inherit subclasses from them.
Even though classes are not in a same inheritance hierarchy still they can follow the same implementation using interface
polymorphism with abstract class is possible only if the child object have same parent which is an abstract class. with same inheritance tree.
so if the objects are not in a same tree then to achieve polymorphism we have to use interface Or we can use interface superclass hierarchy to make it possible which is not possible with abstract class
Confusing