Inheritance helps you avoid duplicate code, and improve your flexibility with polymorphism.
A superclass is sometimes called a base class or parent class, and a subclass is sometimes called a derived class or child class.
Use IS-A to test your class hierarchy
Simply ask yourself: “Does it make sense to say type X IS-A type Y?” If it does, then both classes should probably live in the same inheritance hierarchy, as the chances are, they have the same or overlapping behaviors. If it doesn’t make sense, then you know that you need to think again.
Declare the superclass and its properties and functions as open
To use a class as a superclass, it must be declared as open. Everything you want to override must also be open.
In Kotlin, you can only inherit from super classes and override their properties and functions if they’ve been prefixed with open.
How a subclass inherits from a superclass?
To make a class inherit from another, you add a colon (:) to the class header followed by the name of the superclass. This makes the class a subclass, and gives it all the properties and functions of the class it inherits from.
If the superclass has a primary constructor, then you must call it in the subclass header or your code won’t compile.
How (and when) to override properties?
You override a property that’s been inherited from a superclass by adding the property to the subclass, and prefixing it with the override keyword.
If you define a property in the superclass using val, you must override it in the subclass if you want to assign a different value to it.
If a superclass property has been defined using var, you don’t need to override it in order to assign a new value to it, as var variables can be reused for other values. You can instead assign it a new value in the subclass’s initializer block.
If a property in the superclass has been defined using val, you can override it with a var property in the subclass. To do this, simply override the property and declare it to be a var. Note that this only works one way; if you try to override a var property with a val, the compiler will get upset and refuse to compile your code.
How to override functions?
The function parameters in the subclass must match those in the superclass.
The function return types must be compatible.
Whatever the superclass function declares as a return type, the overriding function must return either the same type, or a subclass type. A subclass type is guaranteed to do anything its superclass declares, so it’s safe to return a subclass where the superclass is expected.
An overridden function or property stays open until it’s declared final.
Inheritance guarantees that all subclasses have the functions and properties defined in the superclass.
Any place where you can use a superclass, you can use one of its subclasses instead.
When you call a function on the variable, it’s the object’s version that responds.
Being able to use one type of object in a place that explicitly expects a different type is called polymorphism. It’s the ability to provide different implementations for functions that have been inherited from somewhere else.
Polymorphism means “many forms”. It allows different subclasses to have different implementations of the same function.
Multiple inheritance isn’t allowed in Kotlin, so each subclass can have only one direct superclass.
Type of Inheritance:
Single Inheritance
Multilevel Inheritance
hierarchical Inheritance
Telegram Group: https://t.me/androidNinjaWarrior
Guide to watch:
00:00 What is inheritance?
12:15 How to inherit a class in Kotlin?
19:19 How to override a property of class in Kotlin?
22:04 How to override a function of a class in Kotlin?
24:15 How to assign value in var variable in child class in Kotlin?
26:26 How to deal with constructor in inheritance in Kotlin?
30:00 How to stop an open function to get overridden furthermore in child classes in Kotlin?
31:14 Type of Inheritance
35:55 How to override a val into a var in Kotlin?
39:30 How to pass an object as argument in function?
#Inheritance #typeOfInheritance #singleInheritance #MultilevelInheritance #overridden #override #propertyOverride #functionOverride #MethodOverride #Inherit #superclass #baseClass #parentClass #childClass #subclass #derivedClass #polymorphism #kotlin #KotlinCourse
Disclaimer- Some content are used for educational purposes under fair use.
Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for "fair use" for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use. All credit for copyright material used in the video goes to the respected Owner.
Thank you guys.