Become a Kotlin Programmer (Part 6)
Hello, everybody!.
Today we will learn about Class, inheritance, override and super keyword on Kotlin but the best form to explain this is programming in this entry I will teach you about each element using an interactive method, we will a create a simple RPG Bash game, because in this type of development we need use each type object mentioned in the beginning of this paragraph.
Learning Class with Class
Well, first we need know, “What is a Class?”, a class is an object that allows to us add methods, properties, interfaces and inherit for create a specific object as in real life, let me show a little a short description, for example, we want to create an object “Human”.
This object has some properties, for example, hp, kind, color, hight, age and we have some methods as Learn, Talk, Run, Walk, and Eat, now we go to implement this object using Kotlin. First, we need to create our class.
Now we’ve created the class, the following step is to add the methods for this class.
In this point, our class has walked, run, learn and eat methods implemented. the next step is to add the properties with the class has been initialized.
Obviously, we can add new properties without declaring as a class initializers, simply you need declare the same in the body of the class.
well, we need refine more this class and create some more methods like for example set_head_armor and set_body_armor, got to implement all the necessaries method for the new properties added.
And here is our class Human created, well if we need instance the same, we need do the following action.
Inheritance
Kotlin allow us to do inheritance between class in a very easy way, now we will make a new class “Character” and our class “Human” inherit of this, we need to move the methods from “Human” to “Character”, because in the future we need to create a class Monster that inherits of “Character”, here we go.
Now our class “Human” will inherit from “Character”, the reason, because the keyword open is before of the word class, is because is we don’t define as an open class Kotlin assumes that it is a final class and will not allow it to inherit it, then our class can use the methods contained in the “Character” class.
Open Keyword
Well, what if we wanted to overwrite the Run function, actually we can’t do that because all the methods in the “Character” class are finals but we can resolve this little problem adding the keyword “open” before the word “fun” on each method, see the example.
Now we can overwrite the method or replace for our method if we want to keep the original method and add new things.
Override and super keyword
if we need do a “super” then we can add new stuff without replacing the entire function, the keyword “override” we allow to us modify any not final function.
Well this is all for today, but in the next entry we will learn about the “final override”, abstract class and interfaces on Kotlin, Stay up, keep learning and enjoy Kotlin, you can see the examples about this entry in the following link: https://github.com/10537/KotlinExcercises/blob/master/src/RPG/RPG.kt