Inheritance in Kotlin
A free video tutorial from Tim Buchalka's Learn Programming Academy
Professional Programmers and Teachers - 842K+ students
4.5 instructor rating • 50 courses • 849,025 students
Learn about inheritance
Learn more from the full courseLearn Kotlin and Create Games Using the LibGdx Library
Become a real games programmer. Create Games Using Kotlin with the LibGDX Game Development Framework.
21:05:55 of on-demand video • Updated February 2020
- Learn how to create your own games
- Understand how to write reusable code that can be reused in other games
- Learn how to create your own tools for game development
- Have learned the Kotlin language
- Understand how to use many useful design patterns
English [Auto] In this video you will learn what is inheritance and how to use inheritance inheritance is one of the fundamental attributes of object oriented programming. It offers you to define a child cause that re-uses or in other words inherits or extents or modifies the behavior of parent class the class whose members are inherited is called the base or the superclass. The class that inherits the members of the base class is called the then invite class or a child class. So class can only inherit from a single class. However inheritance is transitive which allows you to define an inheritance hierarchy for a set of types. In other words for example type D can inherit from Type C which inherit from Type B again which inherits from the base class type A because inheritance is transitive. The members of type A are available to type D. Inheritance is used to express an is a relationship between a base class and one or more child costs where the child classes are specialized versions of the base class. They divide the class is a type of the base class. For example the enemy class represents any kind of enemy and the pike and it passes represent specific types of things. Now all these can seem a bit confusing. But it is really easy. You'll see through examples. So let's say our game has different enemies. For example pikemen and Archer they both have some common behavior all all. For example bolt and pull but they can also take that weapon. The common behavior goes to the base class or bench press. So let's create our base class for our enemies in all its enemy. The classes will be pikemen and such. So let's first remove our code inside the main and let's remove other classes that we have inside this fight. So first is to declare a class in class and I think it has a VAR health title in the draft Can you cast a BORGU weapon. Which type. And now in this class we will use int to print that in it is called. So in that block in interlock we will just kill him and Sprint and I mean and he called and now we will at a function at that point. But since both our classes will be able to tell I am not taking a weapon let's use those weapons. So we print the weapon that the class is using or our enemy is used to extend the class in Codlin views. So to be able to extend some class important we need to Mark Klaas with open Keyworth that makes us open for extending by default in thing. Every class is fine. And we can't extend it. They actually only open classes can be extended. So we need to create class pikeman first. So now when we create class pipeline it needs Panamic help as in and it will have our. So what are they asking. And now we want to extend it. I mean we just use all the all constructor of and then we going to base our health parameters and parameter for Weapon B. And now we have these curly braces. It costs a lot but we have the compilation error on me. And notice the type is final. So it cannot be in kids that we can't extend. And I mean unless the market is open so important we need to Mark Klaas open to be able to extend So for calling parent constructor here we are calling parent constructor and me which is also a primary constructor and our department as well has a primary constructor parameter killed and parameter so pipe in his arms and let's it ynot the department foot in it and pikeman in it so we can see in the causal the flow and what gets old first. So now we need the. So let's create our clocks are just. We both have health. We will proceed to consider and it will come up again we are explaining the enemy and at the same time calling the parents constructive withheld and not is that pop up. So the outcome will be our health. But I'm a third of it will be both. And again we will just add in a lot of insight into what are called so to see a ghost writer he or we will use a class hierarchy top but first note is there when we have open class there is a little icon. Oh and the saying is subclassed by our child. And so when we click on this i can we can see Argile and pikemen are child classes of the enemy class and we can just click on one of them and our cursor will move to that cause. So we see the quest kiter for us. We can just click on our triplets and go to navigate and there is a class hierarchy or a type hierarchy which you can show with control. So you can see that our child had a parent enemy and enemy has a parent object from Java and link back. But in courting all classes have a common superclass and this is called a me and that is default super for a class with not super types the class. But let's see a subtypes hierarchy. So Art doesn't care any subtypes. But if we click on any and again we need to go to class hierarchy on press control page. Now you can see that enemy has achieved pikemen and now it's closed is hierarchy. And inside the main method we're going to create pikemen And and so while the bike went in constructor we have the best health and armor so we can to be handled and will be one by one. Got a tech so we can call a tech method. Even with this tech method is the quote enemy says we are extending enemy with pikeman. We can also inherit and we are automatically inheriting and having access to all the methods and all the attributes of them including all the properties that are easy to part with help under it and let say 5 will be a recount. And then we can again call thought that. So let's run the code and see control the flow of our code. So in console first you will see enemy in court. Why is the enemy called First Cause. Well we are calling the pikemen construct. So if you contemplate on the pikemen constructor you will see that we are extending it to me and that means we are also calling the enemy construct. So again called an enemy constructor. When this constructor gets executed. Unit block is X so that is why the enemy in it is called first then the pikemen in is called and when we are calling a tech that it prints are taken with back with the true. It is the same principle plinths enemy in it called Archer. It's called and attacking with both. So this is inheritance where we are extending the base class LME and you will notice that if we type pikeman don't care access to health armor and weapon be torture. We have access to public health but we don't count on because Arment is just declared property inside the pipe. But health and property repoint are shared. Actually they are not shared. They are inherited in bold. Of course I will see you in the next video.