Wednesday, May 20, 2020

Inheritance,Overiding,Visibility Modifier, Abstract class & Interface

Inheritance :-

Default class in kotlin :- Public and Final(member can't derived)
make class open for Inheritance

eg-1

open class ABC {  //parent class (open)
   fun think () {
      print("Hey!! i am thiking ")
   }
}
class BCD: ABC(){ // inheritence happend (child class)
}

class MCD:ABC() {  //Inherit parent class method also have its own method
fun doing() {
print("I am doing")
}

fun main(args: Array<String>) {
   var  a = BCD()
   a.think()
   var b =MCD()
   b.think()
   b.doing()
}


Overiding :-
child class method want to work in its own ways
super is used to call parent method


open class Animal{
open var color : Stirng = "White"
   open fun eat () {
      print("Animal Eating")
   }
}

class dog: Animal() { // inheritance happens
var breed : String = "labrador"
override var color = "Black"

fun bark()
{
print("Dog barking")
}
   override fun eat() {
   super<Animal>.eat()   //executing parent method also
      print("Dog Eating")
   }
}
fun main(args: Array<String>) {
   var  doga = dog()
   doga.eat()
   doga.bark()
}


Overiding in Constructor :-

Primary Constructor

main()
var dog =  Dog("Black","png")

open class Animal(var color:String)
init {
print("from animal Init : $color) }}

class dog (color:String, var breed:String):Animal(color) {
init {
print("from Dog Init : $color and $breed) }}

Secondary Constructor

open class Animal {
var color : String = " "
constructor(color : String) : this() {
this.color = color }}

class Dog: Animal {
var breed:String =  " "
constructor(color: String, breed: String): super(color) {
this.breed = breed }


Visibility Modifier

Public -  By Default in Kotlin
Protected - Not applicable to High level Function or classes - Visible in sub class
Internal - Visible within same  module (within project)
Private - Visible inside ____.kt


open class Person {
private val a = 1
protected val b = 2
internal val c = 3
val d  = 10 //public by default }

within Inherit class

class India : Person() {
// a is not visible
//b,c,d are visible
}

within different class in same package

class Textclass  {
//person.a, person.b are not visible //private and protected
//person.c, person.d are visible //public and internal  }


Abstract - Classes/Method/Properties :-
-can't create instance/object of Abstract class
-need to overide Abstract method,properties inside Derived class

abstract class Person {   //can defined instance of abstract class
  abstract var name : String
  abstract fun eat()       //abstarct properties is open by default
  open fun getHeight() { }  //'open' function ready to overridden
  fun gotoSchool()   { } //Normal funtion public & final by default
}

class Indian: Person() {
overide var name : String
overide fun eat() {
//own code
}
}


Interface :-
-it is not class
-can not Instantiated
-have 2 method with same name, we have to overide them.
        super<interfacename>.method
-can normal and abstract method

main() {
var myButton = MyButton()
myButton.onTouch()
myButton.onClick() }

interface MyInterfaceListener {   //can't create Instance
fun onTouch()  //Method in interface is Abstract by Default
fun onClick()  {  //Normal method are public and open by Default , but not final
println("FirstInterface :- on click") }

interface MySecondInterface {
fun onTouch() {
println("SecondInterface :- on Touch")
fun onClick()  {
println("SecondInterface :- on click") }


class MyButton :  MyInterfaceListener,MySecondInterface  {
overide fun onclick() {
super<MyInterfaceListener>.onClick()
super<MySecondInterface >.onTouch() }

overide fun onTouch() {
super.onTouch()  }}  //executes by order, takes first Interface




No comments:

Post a Comment

Null Safety , Lazy Keyword and LateInit Keyword

Null Safety :- Safe call  ( ? )    use it if you don't mind getting null values Not Null Insertion ( !! )  use if you are sure that val...