SlideShare une entreprise Scribd logo
1  sur  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 }

Contenu connexe

Similaire à Kotlin OOP Concepts

Lecture_4-Class and Object.pptx
Lecture_4-Class and Object.pptxLecture_4-Class and Object.pptx
Lecture_4-Class and Object.pptx
ShahinAhmed49
 

Similaire à 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
 
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
 
29c
29c29c
29c
 
29csharp
29csharp29csharp
29csharp
 
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
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdf
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

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 }