Prepared By : Prof. Uday Shah (HOD -IT)
Unit 2 : Kotlin Programming Language , class and Inheritance
1.
Introduction to Kotlin. 1
2.
Basic Syntax and Data Types. 2
3.
Variables. 2
4.
Conditions. 3
5.
Control Flow and Loops. 3
6.
List and Array. 4
7.
Function and Classes in Kotlin. 5
8.
Constructor. 5
9.
Inheritance. 6
10.
Interface. 6
11.
Enum.. 7
- Kotlin
is a modern, statically-typed programming language developed by JetBrains.
- It is
fully interoperable with Java, meaning Kotlin code can use Java
libraries and vice versa.
- Officially
supported by Google for Android app development since 2017.
- Kotlin
code is concise, expressive, and safe from common programming errors.
- It
runs on the Java Virtual Machine (JVM) and can also compile to
JavaScript or native code.
- Offers
features like null safety, extension functions, and coroutines.
- Reduces
boilerplate code compared to Java.
- Strongly
supports object-oriented and functional programming
paradigms.
- Uses
smart casts and type inference to simplify coding.
- Kotlin
files typically have the .kt extension.
- Provides
enhanced readability and maintainability.
- Supported
by Android Studio with full tooling support.
- Used
not only for Android but also for backend (Ktor), web, and multiplatform
projects.
- Kotlin
syntax is simple and expressive, similar to modern languages like Swift or
Python.
- Every
Kotlin program starts with a main() function.
- Statements
do not require semicolons (;) — optional.
- Uses val
for read-only variables and var for mutable ones.
- Data
types include Int, Double, Float, Boolean, Char,
String.
- Kotlin
has type inference, so you can omit explicit types in most cases.
- Example:
val age = 20 automatically detects Int.
- Strings
can be concatenated or formatted using ${variable} syntax.
- Supports
multi-line strings using triple quotes ("""...""").
- Conversion
between types uses .toInt(), .toDouble(), etc.
- Variables
are non-null by default; nullable ones must be declared with ?.
- Example:
var name: String? = null allows null assignment.
- Consistent
syntax makes code easier to read and debug.
- Variables
are storage containers for data in Kotlin.
- Declared
using val (immutable) or var (mutable).
- Example:
val x = 10 (cannot change), var y = 20 (can change).
- Kotlin
automatically infers data type when assigned a value.
- Variables
can store primitive or object data types.
- Support
lateinit for delaying initialization in classes.
- Constant
values can be defined using const val at the top level.
- Kotlin
promotes immutability — using val for safer code.
- Nullable
variables allow storing null values safely using ?.
- Supports
string interpolation — "Hello, $name".
- Global
variables can be declared outside functions.
- Variables
can be initialized conditionally using if or when.
- Improves
memory efficiency and program safety.
- Used
to make decisions and control program flow.
- if
and else are the primary conditional statements.
- Example:
- if (age >= 18)
println("Adult") else println("Minor")
- if
can also return a value, unlike in Java.
- when
is Kotlin’s replacement for the switch statement in Java.
Example:
when(day) {
1 -> println("Monday")
2 -> println("Tuesday")
else -> println("Other Day")
}
- Supports
multiple conditions in one branch (in, !in, is, !is).
- Makes
code more readable and structured.
- Nested
conditions are supported for complex logic.
- Conditions
can handle both expressions and statements.
- Logical
operators like &&, ||, ! are used for combining conditions.
- Simplifies
decision-making and improves control over logic flow.
- Control
flow allows repetitive tasks or branching execution.
- Common
loops: for, while, do-while.
- for
loop can iterate over ranges, arrays, or collections.
- for (i in 1..5) println(i)
- while
loop runs while a condition is true.
- do-while
ensures the loop runs at least once.
- Supports
loop control statements: break, continue, and return.
- Can
iterate backward using downTo and skip steps using step.
- for (i in 10 downTo 1 step
2)
- Supports
labeled loops for nested iterations.
- Allows
combining with conditions for dynamic execution.
- Loops
and control flow help manage logic and repetitive operations efficiently.
- Array:
Fixed-size collection of same-type elements.
- val numbers = arrayOf(1,
2, 3, 4)
- Access
elements using index: numbers[0].
- Kotlin
provides built-in functions like size, contains(), first(), etc.
- List:
Ordered collection, can be mutable or immutable.
- val list =
listOf("A", "B", "C") // immutable
- val mList =
mutableListOf("A", "B", "C") // mutable
- Supports
iteration using loops or forEach().
- Arrays
are fixed in size; lists can dynamically grow.
- Supports
functional operations like map, filter, sorted, etc.
- Can
mix different data types using Any.
- Provides
null safety with optional types.
- Lists
and arrays are essential for data storage and manipulation.
- Functions
are reusable blocks of code that perform specific tasks.
- Declared
using fun keyword:
- fun add(a: Int, b: Int):
Int = a + b
- Supports
default and named arguments.
- Can
return values or perform actions (Unit type).
- Classes
are blueprints for creating objects.
- Declared
using the class keyword.
Example:
class Student(val name: String, val
age: Int)
- Supports
encapsulation, inheritance, and polymorphism.
- Members
include properties, methods, and constructors.
- Classes
and functions together support modular programming.
- Used
to initialize class objects.
- Two
types: Primary and Secondary constructors.
- Primary
constructor is defined with class header:
- class Person(val name:
String, var age: Int)
- Secondary
constructor provides alternate ways to create objects.
- constructor(name: String):
this(name, 0)
- init block
runs automatically after the primary constructor.
- Constructors
help assign initial values to properties.
- Provide
flexibility in object creation.
- Default
values can simplify initialization.
- Support
inheritance through super() calls.
- Ensure
object setup before use.
- Allows
one class to acquire properties and methods of another.
- Base
class (parent) and Derived class (child).
- Declared
using the open keyword since classes are final by default.
open class Animal {
fun eat() { println("Eating...") }
}
class Dog: Animal() {
fun bark() { println("Barking...") }
}
- Supports
method overriding with override keyword.
- Promotes
reusability and reduces redundancy.
- Enables
polymorphism — same interface, different behavior.
- Child
class can access parent’s public and protected members.
- Constructors
can be inherited and customized.
- Follows
the IS-A relationship.
- Core
part of OOP in Kotlin.
- Defines
a contract of methods that implementing classes must follow.
- Declared
using the interface keyword.
- interface Vehicle {
- fun start()
- }
- A
class implements it using the : symbol.
- Interfaces
can contain abstract methods and default implementations.
- A
class can implement multiple interfaces (supports multiple inheritance).
- Provides
flexibility in design and reduces code dependency.
- No
state or constructors allowed in interfaces.
- Used
to achieve abstraction and polymorphism.
Example:
class Car: Vehicle {
override fun start() { println("Car started") }
}
- Helps
in designing loosely coupled systems.
- Enum
(Enumeration) defines a fixed set of constant values.
- Declared
using the enum class keyword.
- enum class Direction {
NORTH, SOUTH, EAST, WEST }
- Each
constant acts as an object of the enum type.
- Enums
can contain properties, methods, and constructors.
- Useful
for representing predefined choices (e.g., days, colors, directions).
- Supports
when expressions for decision-making.
Example:
when(direction) {
Direction.NORTH -> println("Up")
else -> println("Other")
}
- Provides
type safety — avoids invalid values.
- Improves
code clarity and maintainability.
- Commonly
used for flags, states, and modes in Android apps.
:: Thank You ::