Understanding OOP Concepts With Kotlin
Tags: kotlin, oop, android, mobile
Published: 2021-07-06 14:04:17

You just finished learning variables in kotlin and you are wondering how do they come together to create complex amazing programs you have read about. This article will introduce you to the concepts of object oriented programming in a simplified way. We will use practical examples to explain how Complex methods are created. So let us dive right in 

<img width="100%" style="width:100%" src="https://media.giphy.com/media/xUNda6q8X3vwXzNvA4/giphy.gif">

First of all we need to understand what Object oriented programming(OOP) is. OOP relies on the concepts of classes and object. it is a way we use to structure our software program into simple pieces that can be used multiple times.

This brings us to another question . What are classes? . A class is a blueprint that is used to create more specific objects. Let us take the case of an architect.

 An architect has a blueprint for how he wants a building to look like. From that blueprint multiple buildings can be built. Each of the building can have a unique name and properties, but they all share the same fundamental features

Let us use a real life example to explain OOP concepts. Let us use the example of a school. Imagine you were awarded the contract to write an application for a school. The aim of the application is to make the school more productive.

Your application will allow the principal admit students, a student can take multiple courses, a teacher can teach a course.. etc. 

Creating a program this complex would require a lot of code. Your school could have thousands of students and it would be terribly inefficient to write unique code for each of the students. We could create a class called student in kotlin 

```
class Student(){
}
```  

Each of the student could have properties, which can include a name, age, class etc.. 
In kotlin properties of class can be declared in the primary constructor using this format 
```
class Student(name: String, age:String , class:String){
}
```
By creating a student class we can create multiple students. In this instance the multiple students are instances of the student class. i.e types of the student class. They are called objects. In kotlin objects are instantiated this way 

```
var samuel = Student("samuel", 23, "Grade 5")
```

Samuel which is an object of the student class has three attributes. which are his name, age and Grade. 

Attributes are information associated with a class . sometimes it will be necessary for a student to perform some actions like take a course or take examinations. In kotlin methods are declared this way

```
fun takeCourse(course:String){
}
```
A method can take in information and perform an action. it can also return information

```
fun takeCourse(course:String): Boolean{

return true
}
```

Now let us explore the four principles of OOP

* Inheritance
* Encapsulation
* Abstraction 
* Polymorphism


## Inheritance

Inheritance is a feature in OOP that allows classes to inherit from other classes. Inheritance supports reusability. it reduces the number of boilerplate code that needs to be written. Let us use the case of our school project. The different roles in a school. i.e teachers, principals, students. etc are all human beings. so they all need to have a name, age etc. 

if we were to use that model this scenario in kotlin we would need to first create a class person and make student, principal and staff class inherit from it. 

```
open class Person(name:String){
}

class Student(name:String): Person(name){
}
```

In the code snippet above class student inherits the name attribute from the Person class

```
open class Person(name:String){
open fun dance(){}
}

class Student(name:String): Person(name){
override fun dance() {  }
}
```
You can also override methods used in the parent class


## Encapsulation

![hide](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqmy1i52lj9gdgtth0o8.jpeg)
Encapsulation is the art of hiding important information from other classes and exposing them only through a select method. Imagine the requirements of our app dictates that the principal should only be able to see the list of all students and add students to the list. we would have to look for a way to make that happen. That way is called encapsulation.

```
class Principal(){
 private var studentsName = mutableListOf<String>()


fun getStudentsName(): List<String>{
  return studentsName
}
```

The names of the students have been hidden and can only be gotten through the getStudentsName method. The only way it can be modified is only within the class.  We can achieve encapsulation through the use of [visibility modifiers](https://kotlinlang.org/docs/visibility-modifiers.html)


## Abstraction 

Abstraction means that the user can only interact with select attributes and methods of an object. In our case if a principal admits applicants and makes them students. An outsider does not care how the principal goes about it. The other parts of the program would only want to get the students or pass the applicant to be converted to students. If there is a complex operation ongoing in the principal class that changes an applicant to student,  other parts of the program do not care about it.

## Polymorphism


![Polymorphism](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kpdkigk3yl9xz99iiiv3.jpeg)

This refers to the art of designing objects to share behaviour either through method overloading or method overriding.

In kotlin these are achieved through the following syntax

```
fun admitApplicant(applicant: String){
}

fun admitApplicant(applicants: List<String>){
}
```

From the code both methods can be named similarly but the method that gets called is dependent on what you passed in as a parameter in this case one method takes a string while the other takes in a list of string. 

If all users in our school application can delete themselves from the database. That means in our Person class there would be a method called delete  



```
open class Person(name:String){
open fun delete(){}
}

class Student(name:String): Person(name){
override fun delete() { 
  //students can delete themselves from the database
 }
}
```


Learning object oriented programming is a continous process that never ends in the life of a developer. Each program you would write during the course of our career would require you to think deep on how best to implement it keeping OOP principles in mind.


If you have any observations or questions you can reach out to me on twitter [@rake_code](https://twitter.com/rake_code)