Kotlin Classes OOPS - Part 1

Kotlin - What is OOP?

OOP stands for Object-Oriented Programming.


Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.


Object-oriented programming has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the Kotlin code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time
  • Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You should extract the common codes for the application, place them in a single place and reuse them instead of repeating them.
..

Kotlin - What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.

Look at the following illustration to see the difference between class and objects:
So, a class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the variables and methods from the class.

..

Kotlin Classes and Objects

Everything in Kotlin is associated with classes and objects, along with its properties and functions. 

For example: in real life, a car is an object. The car has properties, such as brand, weight, and color, and functions, such as drive and brake.

A Class is like an object constructor or a "blueprint" for creating objects.

..

Create a Class

To create a class, use the class keyword, and specify the name of the class:

Example

Create a Car class along with some properties (brand, model, and year)

class Car {
  var brand = ""
  var model = ""
  var year = 0
} 

A property is basically a variable that belongs to the class.

..

Create an Object

Now we can use the class named Car to create objects.

In the example below, we create an object of Car called c1, and then we access the properties of c1 by using the dot syntax (.), just like we did to access array and string properties:

// Create a c1 object of the Car class
val c1 = Car()

// Access the properties and add some values to it
c1.brand = "Ford"
c1.model = "Mustang"
c1.year = 1969

println(c1.brand)   // Outputs Ford
println(c1.model)   // Outputs Mustang
println(c1.year)    // Outputs 1969 

..

Multiple Objects

You can create multiple objects of one class:

val c1 = Car()
c1.brand = "Ford"
c1.model = "Mustang"
c1.year = 1969

val c2 = Car()
c2.brand = "BMW"
c2.model = "X5"
c2.year = 1999

println(c1.brand)  // Ford
println(c2.brand)  // BMW

..

Kotlin Constructor

In Kotlin, there's a faster way of doing this, by using a constructor.

A constructor is like a special function, and it is defined by using two parantheses () after the class name. You can specify the properties inside of the parantheses (like passing parameters into a regular function).

The constructor will initialize the properties when you create an object of a class. Just remember to specify the type of the property/variable:

class Car(var brand: String, var model: String, var year: Int)

fun main() {
  val c1 = Car("Ford", "Mustang", 1969)
}

..

Kotlin Class Functions

You can also use functions inside a class, to perform certain actions:

Create a drive() function inside the Car class and call it:

class Car(var brand: String, var model: String, var year: Int) {
  // Class function
  fun drive() {
    println("Wrooom!")
  }
}

fun main() {
  val c1 = Car("Ford", "Mustang", 1969)
  
  // Call the function
  c1.drive()
}

Tip: When a function is declared inside a class, it is known as a class function or member function.

Note: When an object of the class is created, it has access to all of the class functions.

..

Class Function Parameters

Just like with regular functions, you can pass parameters to a class function:

Create two functions: drive() and speed(), and pass parameters to the speed() function:

class Car(var brand: String, var model: String, var year: Int) {
  // Class function
  fun drive() {
    println("Wrooom!")
  }
  
  // Class function with parameters
  fun speed(maxSpeed: Int) {
    println("Max speed is: " + maxSpeed)
  }
}

fun main() {
  val c1 = Car("Ford", "Mustang", 1969)
  
  // Call the functions
  c1.drive()
  c1.speed(200)
}

..

Kotlin Inheritance (Subclass and Superclass)

In Kotlin, it is possible to inherit class properties and functions from one class to another. We group the "inheritance concept" into two categories:


subclass (child) - the class that inherits from another class

superclass (parent) - the class being inherited from

In the example below, MyChildClass (subclass) inherits the properties from the MyParentClass class (superclass):

// Superclass
open class MyParentClass {
  val x = 5
}

// Subclass
class MyChildClass: MyParentClass() {
  fun myFunction() {
    println(x) // x is now inherited from the superclass
  }
}

// Create an object of MyChildClass and call myFunction
fun main() {
  val myObj = MyChildClass()
  myObj.myFunction()
} 

Why And When To Use "Inheritance"?

- It is useful for code reusability: reuse properties and functions of an existing class when you create a new class.

Above Example Explained

Use the open keyword in front of the superclass/parent, to make this the class other classes should inherit properties and functions from.

To inherit from a class, specify the name of the subclass, followed by a colon:, and then the name of the superclass.

..

Comments