A summary of OOP concepts in Kotlin with easy examples.
What is Kotlin?
Kotlin is a modern, trending programming language that was released in 2016 by JetBrains. It has
grown in popularity because it is compatible with JAVA (one of the most popular programming
languages).
Kotlin is used for:
• Mobile applications (specially Android apps)
• Web development
• Server-side applications
• Data science
Refer https://kotlinlang.org/docs/classes.html for more examples.
2. Classes and Objects
• Classes : Blueprint of objects.
• Object : An entity having attributes(name,age) or behaviours or
instance of the class binding data and function together in the
single unit.
parrot2 = Parrot()
parrot2 is an instance of Parrot class
3. Classes
class Parrot{ /*...*/ }
• The class declaration consists of the class name, the class
header (specifying its type parameters, the primary constructor,
and some other things)
• Class body surrounded by curly braces.
• Both the header and the body are optional.
• If the class has no body, the curly braces can be omitted.
4. Constructors
• Constructor is often used to initialize class members.
• Every kotlin class has a default constructor available if no
constructor is defined.
5. Constructors
• Primary and Secondary
• Rules of Primary constructor:- 1. No body
2. The init block acts as the body
• Rules of Secondary constructor:- 1. Has a body
2. Body of constructor is called
after the init
block.
6. Primary Constructor
• Part of the class header, and it goes after the class name and optional type
parameters.
class Parrot(Name: String) { /*...*/ }
• Inside the primary constructor we cannot write any logic, and to write the logic
we use init blocks in kotlin.
• A Kotlin class can have only single primary constructor and as many init blocks
and secondary constructors as required.
7. Inheritance
• Inheritance : Deriving classes from an existing class.
• The child classes
• Derived using an existing class inherits the properties and implementations
from the parent class;
• So, we do not have to define the implementations of parent class again.
• Hence, inheritance helps us in reusing the existing code.
• By default, Kotlin classes are final – they can't be inherited. To make a class
inheritable, mark it with the open keyword:
open class Base // Class is open for
inheritance
8. Inheritance
Class Animal{
}
Class Dog{
Var color: String
Var breed: String
Fun bark() {
}
Fun sleep(){
}
}
Class Cat{
Var color: String
Var age: Int
Fun meow() {
}
Fun sleep(){
}
}
*See how to override methods
9. Interfaces
• Interfaces are a way of defining abstractions.
• An interface is a collection of method and property.
10. Properties
• Encapsulates a value or a behavior associated with an object of
that class and are mutable.
• Properties are declared using the val or var keyword.
class Address {
var name: String = "James"
var street: String = "Brown"
var city: String = "Helsinki"
}
11. Sealed Class
• Subclassed in the same file.
• Commonly used to represent restricted class hierarchies(Where a
value can have one of the limited set of types).
Ex: - Representing different types of errors:
➔ sealed class called AppError
➔ subclasses like NetworkError, DatabaseError, and
PermissionError to represent different types of errors that can
occur in your app.
sealed class IOError(): Error
12. Enum Class
• Represents a fixed set of values.
• Used to define a set of constants. (A situation that does not
change)
Ex:- Enum classes can be used to encapsulate related values
into a single type.
enum class Seasons{
SPRING, SUMMER, AUTUMN, WINTER }