KOTLIN 1.101.2: Getting Started with Kotlin (OOP)
Sarp Remzi Aksu
Certified Java/Kotlin & Android, Swift & iOS, JavaScript Expert | GDG Adana Co-Organizer
Before I start, I can hear some questions about this topic.
FAQ 1: What is "OOP"? What does it have to do with Kotlin?
To be clarified, objects are the foundation for numerous modern languages, including Kotlin. In an object-oriented programming language, you discover “nouns” in the problem you’re solving, and translate those nouns to objects. Objects hold data and perform actions. An object-oriented language creates and uses objects and classes. By encapsulating the fields and functions, we can create the instance or object of the class and then utilize the fields and functions to meet our programming requirements.
Now let's take a look about topics of Object Oriented Programming.
1) Classes and Objects
To create a class, you can use the class keyword, and specify the name of the class such as example below:
class Person { }
Consider that we have an empty class now. Now let's fill it up with contents. Adding fields and functions in our class makes this class fully operative class.
class Person { var name = "" var age = 0 var meal = "" fun eat() { println(name + " is eating " + meal + ". He is " + age + " years old.") } } fun main() { val p = Person() p.name = "Ahmet" p.age = 20 p.meal = "Doner Kebab" p.eat() }
This is a basic example of OOP in Kotlin. With object "p", you can operate the fields and functions which were declared on classes. In summary, we need to abstract things and encapsulate into class, then we need to define the properties as fields and capabilities as functions, then we instantiate the class and use the fields and functions as needed.
2) Inheritance
Now let us start to learn another important concept of OOP which is inheritance. Inheritance follows the concept of biological inheritance. In other words, you will make new classes from an existing class but with new features on them. You can use inheritance as the example below:
open class baseClass (x:DataType) { (..) } class derivedClass(x:DataType) : baseClass(x) { (..) }
In Kotlin, all classes are final by default. To permit the derived class to inherit from the base class, we must use the open keyword in front of the base class.
When we inherit a class then all the properties and functions are also inherited. We can use the base class variables and functions in the derived class and can also call functions using the derived class object.
For example:
open class baseClass{ val name = "Mr. Sarp" fun base(){ println("This is a base class.") } } class derivedClass: baseClass() { fun derived() { println(name) println("This is a derived class.") } } fun main(args: Array<String>) { val inh = derivedClass() inh.base() inh.derived() }
领英推荐
3) Interfaces
Although it would be a bit hard to understand our last topic, because it's a bit complicated, this section will be much simpler since interface in Kotlin is almost identical as in Java.
Interface is an important concept when it comes to understand polymorphism. It is known that Java only supports single inheritance which means that any class can only inherit one super class while being able to implement any number of interfaces. Kotlin is exactly like Java in this situation.
In Kotlin, an interface is a collection of abstract methods and properties that define a common contract for classes that implement the interface. An interface is similar to an abstract class, but it can be implemented by multiple classes, and it cannot have state.
For example:
interface Automobile { fun start() fun stop() } class Car : Automobile { override fun start() { println("The car's engine is started.") } override fun stop() { println("The car's engine is stopped.") } } fun main() { val auto = Car() auto.start() auto.stop() }
In this program, the interface "Automobile" declares two methods: start() and stop(), which need to be overridden. The class "Car" implements the interface using the class-literal syntax and overrides the two methods using the override keyword. Finally, the main function creates an object of class Car and calls the two methods.
Interfaces in Kotlin have their own properties and they can be implemented too. Since classes in Kotlin follow the concept of single inheritance, that is, each class can inherit only class, however, in case of interfaces a class supports multiple inheritance, also known as multiple conformance in Kotlin. A class can implement more than one interface, provided that it provides a definition for all the members of the interface.
For example:
interface Properties { val a : Int val b : String get() = "Hey there! I'm using WhatsApp!" } interface Methods { fun desc() } class MultiInterface : Properties, Methods { override val a : Int get() = 315269 override fun desc() { println("Multiple Interfaces has been implemented") } } fun main() { val multi = MultiInterface() multi.description() }
FAQ 2: What will be the advantages and disadvantages of using OOP principles in Kotlin?
OOP principles help you reuse your code for better productivity. With accessibility you can share data between classes thus reducing code duplication. With encapsulation, you will protect an object by unwanted access by other client thus increases program security. With abstraction, you can make your program more maintainable and less prone to bugs. BUT using OOP principles makes your program more complex thus makes program less understandable and changeable at the first moment.
FAQ 3: Which resources should be helpful for me to understand OOP principles in Kotlin?
As I recommended before, you can check upon Kotlin in Action by Dmitry Jeremov & Svetlana Isakova and Atomic Kotlin by Svetlana Isakova & Bruce Eckel. These books provide a comprehensive introduction to the Kotlin programming language, including its syntax, libraries, and best practices, with many hands-on examples and exercises.
In conclusion
In Kotlin, using OOP principles really helps you develop more detailed and complex programs to make it work for many services.
I'll be starting to explain Android app components next week in order to make you understand learning the fundamentals of Android app development better.
See you soon!