In this course, we'll learn about object-oriented programming and its implementations.
- Method Overriding
- Super and This Keywords
- Final Keyword
- Abstract Classes
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we'll talk about the method Overriding. If you remember, we talked about method overloading in the previous videos. If the methods have the same name but different parameters, this is called method overloading. Well, what is the method overriding?
If you define a method in a subclass that is already defined in the superclass with the same name, same parameter, and same return type, it is known as method overriding. You got it? Same name, same parameter, same return type. In this case, the method in the superclass is called overridden method and the method in the subclass is called the overriding method. Let's take a simple example to understand better. We have two classes: a superclass Vehicle and a subclass Car, and the Car class extends the Vehicle class. Both two classes have a common method; void stop(). Car class gives its characteristic to the stop() method, so it overrides the stop() method. It prints, 'Car has stopped,' instead, 'Vehicle has stopped'.
Now, let's make some examples to understand method overriding. In the object oriented programming project, I'll right click on the source folder and select New Class. I'll specify the package name as methodoverriding and the class name will be Vehicle. The Vehicle is our superclass. Let's declare three public void methods in Vehicle class. The first one is the public void start(). We print 'The vehicle has started' message by using the print method. The second is the public void accelerate(). This method takes one int parameter named speed. We print, vehicle accelerates according to given speed. System.out.printIn( ''The Vehicle accelerates at'' ). I will use the speed variable in the print method. So, after the plus sign I write, speed. The last method will be stop. In the stop method, we print the ''Vehicle has stopped'' message by using the print method.
We save the code. Okay, let's create a new subclass. I'll on the methodoverriding package and select New and Class. Specify class name as Car. In order to implement method overriding, we have to add the extends keyword after subclass Car. Our subclass Car extends superclass Vehicle. This means that subclass Car inherits all methods from superclass Vehicle. Car class is empty now. The question is, why we need method overriding. Let me show the answer in this question. Let's create the main test class named OverrideTest in the same package. In the main method, we create a Vehicle object; Vehicle vehicle = new Vehicle(). First, I will call the start method from the Vehicle class. And then I'll call the accelerate method by passing the value 80 of a speed as a parameter and the last, I will call the stop method. Okay, let's run the code. You see the results in the console. The vehicle has started, the vehicle accelerates at speed 80 and the vehicle has stopped.
Let's create a Car object to test; Car myCar = new Car(). Now I will call the start method, and then call the accelerate method by passing the value 100 of speed as a parameter and call the stop method. Okay, let's run the code. You see the same results; vehicle has started, vehicle accelerates at speed 100, and vehicle has stopped. But there is a problem, we wait for properties of car, not vehicle in the second part. This is where overriding comes into play. So, we need to override the methods of superclass Vehicles in the subclass Car. Thus, we have the chance to shape the same method according to our own class. Let's override these methods. I'll open the Vehicle's class. Let's copy the methods of superclass Vehicle, and I will paste them into the subclass Car and let's make changes for implementing properties of the Car class. I will print, ''Car has started'' in the start method instead of vehicle.
I will print, ''Car accelerates according to given speed'' instead of vehicle, and I will print, ''Car has stopped'' in the stop method instead of vehicle. These three methods have the same name, same parameter, and same return type as methods in the Vehicle class. This is a rule for overriding; the same return type, same name, same parameter. We save the code. Okay, let's open the main class OverrideTest and let's run the code. Now, you see two different messages coming from the methods of classes. In the first part, we display messages from superclass Vehicle. In the second part since we override the methods of superclass Vehicle in class Car, 'Car has started', 'Car accelerates at speed 100', and 'Car has stopped' messages are displayed. Okay, before concluding the lesson, I'd like to draw your attention to one more small point.
The rule for overriding must be the same return type, the same method name, and the same parameter requirement. What about access modifier? Could it be an access modifier difference? If you notice in our example, the access modifiers of the methods in both the superclass and subclass are the same. Let's try to change them. Let's move on to the Car class first. For example, let's make the start method private. As you can see we are getting an error. It says 'Cannot reduce the visibility of inherited method from Vehicle.' If we change this to protected, we'll get the same error again. The reason for this is that there is a ranking among the access modifiers as we have mentioned before. The most restrictive is private and the least restrictive is public. Therefore, if you define the methods in the superclass as public, then the methods in the subclass must also be public. Otherwise, you will get this error. But the following is true.
f you make the methods access modifier protected or default in the Vehicle class, you have the chance to change it here as well but if you make it private, you cannot inherit this time. Let's see this right on the example. For example, let's define the start method in the Vehicle class as default. Now let's switch to the Car class and change the modifier of the start method. If we make it public, it works. If we make it protected, it will still work. If we make it default, it'll still work. But if we make it private, it will not work in this case and we will get a compilation error. As a result, the return type, method name, and parameter must be the same for overriding. The access modifier need not be the same, but the restrict order is important. The most restrictive is private, then default, then protected, and the least restrictive is public. Yes, after mentioning this important point, we can end our lesson. I hope that you understood the method overriding subject clearly. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.