Inheritance in OOPs is extending a class by adding new features and modifying existing features while maintaining backward compatibility. The existing class is called the super class while the newly created class is called the sub class.
Here is a short example
B is inherited from A
open class A {
override fun toString(): String {
return "Object of class A"
}
}
class B: A() {
}
fun main(args:Array<String>)
{
var a:A=A()
var b:B=B()
println(a)
println(b)
}
Look up the code closely. Since there is no toString method in the sub class B so it takes its definition from A.
Let us modify the code and add a toString to B as well.
open class A {
override fun toString(): String {
return "Object of class A"
}
}
class B: A() {
override fun toString(): String {
return "Object of class B"
}
}
fun main(args:Array<String>)
{
var a:A=A()
var b:B=B()
println(a)
println(b)
}
Check the highlighted portion of the code.
What if we want to add the output of the A class toString to B?
open class A {
override fun toString(): String {
return "Object of class A"
}
}
class B: A() {
override fun toString(): String {
return super.toString() + " Object of class B"
}
}
fun main(args:Array<String>)
{
var a:A=A()
var b:B=B()
println(a)
println(b)
}
Let us add a variable each to the super class A and the sub class B
open class A {
var aInClassA:String ="I am a variable in class A"
override fun toString(): String {
return "Object of class A, var A=$aInClassA"
}
}
class B: A() {
var bInClassB:String ="I am a variable in class B"
override fun toString(): String {
return super.toString() + " Object of class B var A=$aInClassA, var B=$bInClassB"
}
}
fun main(args:Array<String>)
{
var a:A=A()
var b:B=B()
println(a)
println(b)
}
Now, we will add a function each to the classes A and B.
open class A {
var aInClassA:String ="I am a variable in class A"
override fun toString(): String {
return "Object of class A, var A=$aInClassA"
}
fun f1()
{
println("Function F1 in A")
}
}
class B: A() {
var bInClassB:String ="I am a variable in class B"
override fun toString(): String {
return super.toString() + " Object of class B var A=$aInClassA, var B=$bInClassB"
}
fun f2()
{
println("Function F2 in B")
}
}
fun main(args:Array<String>)
{
var a:A=A()
var b:B=B()
a.f1()
b.f2()
}
What happens if I call f1 from the object of type B
open class A {
var aInClassA:String ="I am a variable in class A"
override fun toString(): String {
return "Object of class A, var A=$aInClassA"
}
fun f1()
{
println("Function F1 in A")
}
}
class B: A() {
var bInClassB:String ="I am a variable in class B"
override fun toString(): String {
return super.toString() + " Object of class B var A=$aInClassA, var B=$bInClassB"
}
fun f2()
{
println("Function F2 in B")
}
}
fun main(args:Array<String>)
{
var a:A=A()
var b:B=B()
a.f1()
b.f2()
b.f1()
}
What happens if I redefine f1 in the B class?
open class A {
var aInClassA:String ="I am a variable in class A"
override fun toString(): String {
return "Object of class A, var A=$aInClassA"
}
open fun f1()
{
println("Function F1 in A")
}
}
class B: A() {
var bInClassB:String ="I am a variable in class B"
override fun toString(): String {
return super.toString() + " Object of class B var A=$aInClassA, var B=$bInClassB"
}
fun f2()
{
println("Function F2 in B")
}
override fun f1()
{
println("Function F1 in B")
}
}
fun main(args:Array<String>)
{
var a:A=A()
var b:B=B()
a.f1()
b.f2()
b.f1()
}
Everything in Kotlin is final by default and you need to mark it as open if you want a class to be inherited or a method to be overwritten
0 Comments