SlideShare a Scribd company logo
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

Similar to Kotlin OOP Concepts

Similar to Kotlin OOP Concepts (20)

[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
 
Java inheritance concept, interface, objects, extends
Java inheritance concept, interface, objects, extendsJava inheritance concept, interface, objects, extends
Java inheritance concept, interface, objects, extends
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
Core java concepts
Core java conceptsCore java concepts
Core java concepts
 
Lecture_4-Class and Object.pptx
Lecture_4-Class and Object.pptxLecture_4-Class and Object.pptx
Lecture_4-Class and Object.pptx
 
29csharp
29csharp29csharp
29csharp
 
29c
29c29c
29c
 
Definning class.pptx unit 3
Definning class.pptx unit 3Definning class.pptx unit 3
Definning class.pptx unit 3
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Java
JavaJava
Java
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
inheritance
inheritanceinheritance
inheritance
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Java
JavaJava
Java
 
encapsulation and abstraction
encapsulation and abstractionencapsulation and abstraction
encapsulation and abstraction
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
Class and objects
Class and objectsClass and objects
Class and objects
 

Recently uploaded

From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...Product School
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...UiPathCommunity
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGuy Korland
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Frank van Harmelen
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Thierry Lestable
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...Sri Ambati
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Product School
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...Elena Simperl
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 

Recently uploaded (20)

From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 

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 }