1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Object Oriented Programming (OOP) in Kotlin

Function Overriding in Object Oriented Programming

Start course
Overview
Difficulty
Intermediate
Duration
1h 31m
Students
11
Ratings
5/5
starstarstarstarstar
Description

This course covers the concept of Object-Oriented Programming in Kotlin, which is a method of designing and implementing software. It simplifies software development and maintenance by providing concepts such as object, class, inheritance, polymorphism, abstraction, and encapsulation. This course will explore those.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.

Prerequisites

This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.

Resources

Course GitHub repo: https://github.com/OakAcademy/Kotlin-Programming-Course/tree/main/ObjectOrientedProgramming

Transcript

Hello, my friends. I'm so glad to see you back. So, now we're going to learn about Function Overriding. If you define a function in a subclass that's already defined in superclass with the same name, same parameter, and same return type. Well, yes, you can do it. It's actually known as function overriding. So, in this case, the function in the superclass is called overridden function, and the function in the subclass is called the overriding function. In fact, I just want to do a simple example here to help understand it just a little bit better.

So, we've got two classes here. A superclass, Vehicle and a subclass, Car. The Car class inherits from the Vehicle class. Both of them have a common function named stop(). The Car class gives its characteristic to the stop function. So, it overrides his stop function. It prints "Car has stopped." instead of "Vehicle has stopped.". You see how that works. We're going to do a few more examples of, so that we can really get this function of overriding. So, first of all, we're going to create a new package and we're going to do the override operations within this package. That's okay if we don't create this package but again, I don't want it to interfere with the classes here. Since, I will be creating similar classes called Vehicle and Car. So, just to make that point, I just want to create a new package Car, alright?

So, here I'll just right click on the project package folder with the mouse, choose 'New Package' option. So, let's give this new packaging name. I'm going to just write override as the name of the package and now we can create a class. Alright, click on 'override' package, choose 'New Kotlin Class/File' as my option and I'll set the name of this class. Make it Vehicle. So, now Vehicle is our superclass. We're going to declare three public unit functions in the Vehicle class. The first function will be start. It print the "Vehicle has started." message by using the printIn function. Second, it's going to be accelerate function. So, this function will take one parameter. The name of the parameter will be speed and it will be of the energy type. We print Vehicle accelerates according to given speed. We use this speed variable in the printIn function and then we'll make the last function stop().  Now, in the stop function, we will print "Vehicle has stopped." as a message just by using the printIn function. So, let's create a new subclass. So, right click on 'overriding' package and select new Kotlin Class/File, specify class name Car. Now, in order to implement function overriding, we have to add the colon sign after subclass Car. Also, we've got to take this Vehicle superclass and make it open. So, our subclass Car inherits from superclass Vehicle and this means that subclass Car inherits all functions from superclass Vehicle. Car class is empty now. So, the question is, well, why do we even need function overriding. Well, let's create a Kotlin file in the same package. So, alright, OverrideTest as the name. And let's create the main function. In the main function, we will create a Vehicle object. I write var vehicle = Vehicle().

First, we call the start function from the vehicle class and then we call accelerate function by passing a value of 80 as the speed and that will be the parameter and last we'll call the stop function. So, what happens when we  run the code? Well, you can see the results in the console. The vehicle has started. The vehicle accelerates at an up to 80 and then the vehicle has now stopped. Alright, you with me. So, let's create a Car object, so that we can test this out. So, we call the start function. Then call the accelerate function by passing the value 100 as the parameter and call the stop function. Now, before running any code, let's make a partition here. This way we'll be able to see the two sections separately in the console. So, what happens when you run the code? You see the same results. Vehicle has started, vehicle accelerates to the speed of 100 and vehicle has stopped. But do you see the problem? We'll weight properties of Car, not Vehicle in the second part. So, we need to override the functions of the superclass Vehicle and the subclass Car. You follow. Let me show you. Let's override these functions. Open the Car class and we'll copy the functions of the superclass Vehicle. Now, let's paste the copied methods into the Car class. So, as you can see Android Studio. Hey, no, no, wait, wait, stop, stop. Because the Car class inherits from the Vehicle class, so it can access these methods directly. Therefore, it considers it a mistake to use these methods here with the same name and the same parameters.

And it's going to tell us if we still want to use these methods here, we must override them. We will do the override process as follows. Alright, we actually need to write override at the beginning of each method before the fun statement here, my friends. Alright, so now let's do this for all three methods. But wait, there's still warnings because methods are also final by default. Now, to resolve this error, all we need to do is make each method open in the superclass, that is the vehicle class. So, why don't we go ahead and do that right now. And as you can see when we make the methods open in the vehicle class, the warnings disappear. Alright, my friends. So, as a result, we override the methods of the vehicle class in the Car class. So, let's make the changes for implementing properties of the Car class. So, we'll print "Car has started." in the start function instead of Vehicle. We print Car accelerates according to given speed instead of Vehicle. And we print "Car has stopped." in the stop function and again instead of Vehicle. So, these three functions have the same name, same parameters, same return type with the functions in the vehicle class. So, now let's open up the OverrideTest file. So, now we can run the code. And what do you see two different messages coming from the functions of classes. In the first part, we display the messages from superclass Vehicle and the second part, since we override the functions of the superclass Vehicle in the class Car. Car has started, car accelerates at speed 100, and car has stopped. These are the messages that are now displayed. So, that's function overriding and I think it's pretty though unclear. Go back and finesse anything if you need to.

But before we close out this section here, I do want to mention this keyword super. So, follow my thinking. What do you think we would do if we want to create a function in the Car class that does the same thing as the functions in the vehicle class, but with a different name. I might have given you too big of a hint or a clue, but yes, we can use the keyword super. Just want to show you a quick example. So, within the Car class, I'll create three functions. The first function be called superStart(). The second function be called superAccelerate(). Let the third function be called superStop(). Alright. So, now let these three functions do the same operation as the three functions in the Vehicle class. For this, inside the first function after typing the keywords super and putting it in a dot, I can display the functions in the Vehicle class. The keywords super here represents the superclass that we inherited the Vehicle class. So, here I choose the start function. Similarly, I'll type super.accelerate(). Finally, I'll write super.stop(). And now inside the main method, let's call these functions. So, here I'm typing car.superStart() first. Now, I'm typing in car.superAccelerate(). Then finally, I'll write car.superStop(). Now, lets also copy and paste this line of code here. Now, let's run and test our code. And there you see it, although the function names that we created in the Car class are different. We have used the super keyword to perform the same operation as the functions of the Vehicle class. Wow, that was packed. So, my friends, thanks for listening but that's how you do an override operation and how you get to use that super keyword. It's a super keyword. Alright, my friends, we're going to take a break here but I want to see you in the next video.

 

About the Author
Students
85
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics