3. ● What is kotlin
● Datatypes in kotlin
● How to use Functions in kotlin
What we have discussed
4. What is OOPs
Object Oreinted Programming is
a programming paradigm based
on the concept of "objects" which
can contain data and code
Encapsulation
Abstraction
Inheritance
Polimorphism
5. Encapsulation
Encapsulation is union data
and logic to one unit and hide
data from external access.
class Animal {
var name: String
var noOfLegs: Int
....
}
7. Creating a class
The objects are created from the
Kotlin class and they share the
common properties and behaviour
defined by a class in form of data
members and member functions
respectively.
class myClass {
private var name: String = "Hello"
fun printMe() {
print("Hello: " + name)
}
}
fun main() {
val obj = myClass()
obj.printMe()
}
8. abstract class Bank {
abstract fun percentage(p: Int)
...
}
class SBI : Bank() {
override fun percentage(p: Int){
println(p/100)
}
}
What is inheritance
Inheritance enables code re-
usability, it allows all the features
from a base class to be inherited
by a derived class. In addition,
the derived class can also add
some features of its own.
9. Inheritance
Base Class
Variables
Public Mode
Derived Derived
Derived
Protected Mode
Private Mode
Public
Private
Protected
Public Private Protected
Not Inherited Not Inherited Not Inherited
Protected Protected
Private
10. Defining an
Abstract Class
A class which can not be
instantiated. This means we
cannot create objects of an
abstract class.
abstract class Bank {
abstract fun percentage(p: Int)
...
}
class SBI : Bank() {
override fun percentage(p: Int){
println(p/100)
}
}
11. What is override and super
Override:
Method overriding means to
redefine or modify the method of
the base class in its derived class.
Super:
The super keyword will call the
constructor of the super or parent
class to initialize the properties of
the parent class
class SBI : Bank() {
override fun percentage(p: Int){
println(p/100)
}
}
12. What is Interface
Interfaces are custom types provided
by Kotlin that cannot be instantiated
directly. Instead, these define a form
of behavior that the implementing
types have to follow
interface Vehicle{
fun start()
fun stop()
}
class Car : Vehicle {
override fun start() {
print("Vehicle started")
}
override fun stop() {
print("Vehicle stopped")
}
}
13. Need of interface?
We can implement multiple interfaces but can't do the same with
abstract class.
Have to specify abstract keyword with a function in abstract class
but in interface its by default.
Interface enables users to learn the system quickly and use it
efficiently.
14. Putting it together with abstract class
abstract class Bank {
abstract fun percentage(p: Int)
}
class SBI : Bank() {
override fun percentage(p: Int){
println(p/100)
}
}
fun main(){
val sbi = SBI()
sbi.percentage(200)
}
Output: 2
15. Putting it together with interface
interface Bank {
fun percentage(p: Int)
{ println("Super Func Call") }
}
class SBI : Bank {
override fun percentage(p: Int){
super.percentage(p)
println(p/100)
}
}
fun main(){
val sbi = SBI()
sbi.percentage(200)
}
Output: Super Func Call
2