SlideShare a Scribd company logo
2
Most read
11
Most read
12
Most read
KOTLIN
OOP CONCEPTS
1. Classes & Objects
2. Constructors
3. Inheritance
4. Properties
5. Sealed Classes
6. Enums
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
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.
Constructors
• Constructor is often used to initialize class members.
• Every kotlin class has a default constructor available if no
constructor is defined.
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.
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.
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
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
Interfaces
• Interfaces are a way of defining abstractions.
• An interface is a collection of method and property.
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"
}
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
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 }

More Related Content

PPTX
Kotlin presentation
PDF
Android Storage - StorageManager & OBB
PPT
Java 8 Streams
PPTX
Functional Patterns with Java8 @Bucharest Java User Group
PPTX
REST & RESTful Web Services
PPTX
Intro to kotlin
PDF
Introduction to kotlin
Kotlin presentation
Android Storage - StorageManager & OBB
Java 8 Streams
Functional Patterns with Java8 @Bucharest Java User Group
REST & RESTful Web Services
Intro to kotlin
Introduction to kotlin

What's hot (20)

ODP
OOP java
PPTX
Basic oop concepts - C++
PPTX
Clean code: SOLID
PPTX
Solid principles
PDF
Spring Framework - Core
PDF
Object oriented programming With C#
PDF
REST APIs with Spring
PPTX
Threading in C#
PPTX
Python ppt
PDF
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
PPT
Inheritance C#
PPTX
Properties and indexers in C#
PDF
Web develop in flask
PPT
Hibernate presentation
PPTX
Java 8 lambda
PDF
REST API and CRUD
PPSX
Spring - Part 1 - IoC, Di and Beans
PPT
Spring Core
PPTX
Introduction to oop
OOP java
Basic oop concepts - C++
Clean code: SOLID
Solid principles
Spring Framework - Core
Object oriented programming With C#
REST APIs with Spring
Threading in C#
Python ppt
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Inheritance C#
Properties and indexers in C#
Web develop in flask
Hibernate presentation
Java 8 lambda
REST API and CRUD
Spring - Part 1 - IoC, Di and Beans
Spring Core
Introduction to oop
Ad

Similar to Kotlin OOP Concepts (20)

PPTX
Introduction to kotlin and OOP in Kotlin
PPTX
Kotlin Language Features - A Java comparison
PPTX
Kotlin
PDF
Intro to Kotlin
PPTX
Kotlin
PDF
2017: Kotlin - now more than ever
PDF
Kotlin: A pragmatic language by JetBrains
PDF
Compose Camp - Intro.pdf
PDF
Little Helpers for Android Development with Kotlin
PDF
Coding for Android on steroids with Kotlin
PDF
9054799 dzone-refcard267-kotlin
PDF
Be More Productive with Kotlin
PDF
What Kotlin Has That Java Does Not?
PDF
Swift and Kotlin Presentation
PPTX
Android Application Development (1).pptx
PDF
Kotlin intro
PDF
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
PDF
Kotlin/Everywhere GDG Bhubaneswar 2019
PDF
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
PDF
Introduction to kotlin for Java Developer
Introduction to kotlin and OOP in Kotlin
Kotlin Language Features - A Java comparison
Kotlin
Intro to Kotlin
Kotlin
2017: Kotlin - now more than ever
Kotlin: A pragmatic language by JetBrains
Compose Camp - Intro.pdf
Little Helpers for Android Development with Kotlin
Coding for Android on steroids with Kotlin
9054799 dzone-refcard267-kotlin
Be More Productive with Kotlin
What Kotlin Has That Java Does Not?
Swift and Kotlin Presentation
Android Application Development (1).pptx
Kotlin intro
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin/Everywhere GDG Bhubaneswar 2019
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
Introduction to kotlin for Java Developer
Ad

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Cloud computing and distributed systems.
PDF
cuic standard and advanced reporting.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
A Presentation on Artificial Intelligence
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Approach and Philosophy of On baking technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
The Rise and Fall of 3GPP – Time for a Sabbatical?
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
sap open course for s4hana steps from ECC to s4
Encapsulation_ Review paper, used for researhc scholars
Per capita expenditure prediction using model stacking based on satellite ima...
Cloud computing and distributed systems.
cuic standard and advanced reporting.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
A Presentation on Artificial Intelligence
Review of recent advances in non-invasive hemoglobin estimation
Approach and Philosophy of On baking technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
Spectral efficient network and resource selection model in 5G networks
The AUB Centre for AI in Media Proposal.docx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm

Kotlin OOP Concepts

  • 1. KOTLIN OOP CONCEPTS 1. Classes & Objects 2. Constructors 3. Inheritance 4. Properties 5. Sealed Classes 6. Enums
  • 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 }