Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin Object Oriented Programming

Avatar for Benson Wamae Benson Wamae
March 23, 2023
30

Kotlin Object Oriented Programming

A brief introduction to Kotlin OOP it assumes you have some basic understanding of the Kotlin language

Avatar for Benson Wamae

Benson Wamae

March 23, 2023
Tweet

Transcript

  1. What is Object Oriented Programming? • Object-Oriented Programming (OOP) is

    a programming paradigm that focuses on the use of objects to represent real-world entities and concepts. • In OOP, an object is an instance of a class, which is a blueprint or template for creating object
  2. Classes • In OOP, a class is a blueprint or

    template for creating objects. • It defines the properties and methods that an object will have, but does not actually create the object itself. class Car(val brandName: String, val engine: Engine, val price: Double) { fun printCarDetails() { println("Car details") println("Brand Name: $brandName") engine.printEngineDetails() println("Price: $price") } }
  3. Objects • In OOP, an object is an instance of

    a class. • A class defines the properties and methods that an object will have, while an object is an actual instance of that class with specific values for its properties.
  4. Properties • In OOP, properties are variables that are defined

    within a class and describe the characteristics of an object. • These properties are accessed and manipulated using the dot notation on an instance of the class. class Car(val brandName: String, val engine: Engine, val price: Double) { fun printCarDetails() { println("Car details") println("Brand Name: $brandName") engine.printEngineDetails() println("Price: $price") } }
  5. Class Members • private means that the member is visible

    inside this class only (including all its members). • protected means that the member has the same visibility as one marked as private, but that it is also visible in subclasses. • internal means that any client inside this module who sees the declaring class sees its internal members. • public means that any client who sees the declaring class sees its public members.
  6. Constructors In OOP, a constructor is a special method that

    is used to initialize an object when it is created. Constructors have the same name as the class and do not have a return type. class Car(val brandName: String, val engine: Engine, val price: Double) { fun printCarDetails() { println("Car details") println("Brand Name: $brandName") engine.printEngineDetails() println("Price: $price") } }
  7. Constructors There can be primary and secondary constructors class Car(val

    brandName: String, val engine: Engine) { var price: Int = 0 constructor(brandName: String, engine: Engine, price: Int) : this(brandName, engine) { this.price = price } fun printCarDetails () { println( "Car details") println( "Brand Name: $brandName") engine.printEngineDetails() println( "Price: $price") } }
  8. Inheritance • In OOP, a class is a blueprint or

    template for creating objects. • It defines the properties and methods that an object will have, but does not actually create the object itself. open class Engine(var engineType: String) { open fun start() { println("Starting engine...") } }
  9. Inheritance class DieselEngine (engineType: String): Engine(engineType) { override fun start()

    { println( "Starting diesel engine..." ) } } class PetrolEngine (engineType: String): Engine(engineType) { override fun start() { println( "Starting petrol engine..." ) } }
  10. Inheritance class ElectricEngine (engineType: String): Engine(engineType) { override fun start()

    { println( "Starting electric engine..." ) } } class HybridEngine (engineType: String): Engine(engineType) { override fun start() { println( "Starting hybrid engine..." ) } }
  11. Interfaces interface EmployeeSalary { fun contractorSalary() fun fulltimeSalary() { //

    optional body } } Interfaces are a way of defining a set of methods that classes can implement in order to provide a specific set of behaviors.
  12. Abstract abstract EmployeeSalary { abstract fun contractorSalary() fun fulltimeSalary() {

    // optional body } } An Abstract class is like an interface but it can hold state
  13. Differences between Interfaces and Abstract classes • An abstract class

    cannot be instantiated, while an interface has no constructor and cannot be instantiated directly. • A class can only inherit from one abstract class, but it can implement multiple interfaces. • An abstract class can have fields and properties with initial values, while an interface can only have constants. • An abstract class can be marked as open to allow subclassing, while an interface is always open by default. • An abstract class is typically used when you want to provide a base implementation for a set of classes that share common behaviors, while an interface is used when you want to define a set of methods that multiple unrelated classes can implement to provide a specific set of behaviors.
  14. Encapsulation This refers to the practice of hiding the internal

    workings of an object from the outside world, and only allowing access to its methods and properties through well-defined interfaces.
  15. Polymorphism Polymorphism is a core concept in object-oriented programming that

    allows objects of different classes to be treated as if they are of a common type. fun testStartEngine(engine: Engine) { engine.start() } val dieselEngine = DieselEngine("Diesel") val petrolEngine = PetrolEngine("Petrol") val electricEngine = ElectricEngine("Electric") val hybridEngine = HybridEngine("Hybrid") testStartEngine(dieselEngine) // Output: Starting diesel engine... testStartEngine(petrolEngine) // Output: Starting petrol engine... testStartEngine(electricEngine) // Output: Starting electric engine... testStartEngine(hybridEngine) // Output: Starting hybrid engine...
  16. Data Class A data class is a special type of

    class that is designed to hold data. It is defined using the data keyword in Kotlin. Data classes are used to create objects that are used to represent data that is being transferred between different parts of an application. data class Person(val name: String, val age: Int)
  17. Data Class: Details The Kotlin compiler automatically generates several useful

    methods for data classes, including: • equals(): compares two objects of the same data class for structural equality. • hashCode(): returns a hash code value for the object, which is used for hashing in hash-based data structures. • toString(): returns a string representation of the object. • copy(): creates a copy of the object with some or all of its properties changed.
  18. Data Class: Details val person1 = Person("Alice", 30) val person2

    = Person("Bob", 25) val person3 = person1.copy(name = "Charlie") println(person1) // prints "Person(name=Alice, age=30)" println(person1 == person2) // prints "false" println(person1 == person3) // prints "false"
  19. Inner Classes and Nested Classes Nested and inner classes are

    used to group related functionality together and improve code organization
  20. Inner Classes • Inner classes are classes declared inside another

    class, but they have access to the instance variables of the outer class • To declare an inner class, use the inner keyword before the class keyword
  21. Inner Class class OuterClass { private val message = "Hello

    from outer class" inner class InnerClass { fun printMessage() { println( "Message from inner class: " ) println( [email protected]) } } }
  22. Nested Classes • Nested classes are classes declared inside another

    class • By default, nested classes are static and cannot access the instance variables of the outer class • You can access the outer class instance using the this@OuterClassName syntax
  23. Nested Classes class OuterClass { private val message = "Hello

    from outer class" class NestedClass { fun printMessage() { println("Message from nested class: ") // Uncomment the following line to access the outer class message // println([email protected]) } } }