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

Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Skynet Technologies
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceSamy Fodil
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewDianaGray10
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPTiSEO AI
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...marcuskenyatta275
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandIES VE
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024Stephen Perrenod
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Paige Cruz
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptxFIDO Alliance
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...ScyllaDB
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?Mark Billinghurst
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdfMuhammad Subhan
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...FIDO Alliance
 

Recently uploaded (20)

Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 

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 }