Method Overriding
Start course
1h 46m

In this course, we will learn the concepts of microservice and spring framework with a focus on inheritance.

Learning Objectives

  • Inheritance in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello there, my friends. In this video, we will talk about method overriding. If the methods have the same name but different parameters, this is called method overloading. So, what is 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, and 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 it 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 of "Vehicle has stopped." So, let's go through an example to better understand method overriding.

In the object-oriented programming project, I will right click on the source folder and select new class. I will 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 one is the public void accelerate. This method takes one int parameter named speed. We print vehicle accelerates according to given speed, System.out.println("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. All right, let's save the code. All right, let's create a new subclass.

I will right click on the method overriding 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 do we need method overriding? I'm going to show you the answer right now. 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 will call the accelerate method by passing the value 80 of speed as a parameter. And lastly, I will call the stop method.

All right, 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. All right, 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 Vehicle 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 will open the Vehicle class. Let's copy the methods of superclass Vehicle and I will paste them into the subclass Car. Let's make the changes for implementing properties for 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. All right, let's save the code. Okay, let's open the main class override test and let's run the code. Now, you see two different messages coming from 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 and class Car, car has started, car accelerates at speed 100, and car has stopped messages are displayed. All right, before concluding the lesson, I would 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. But what about the access modifier? Could there 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. So, 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 the inherited method from Vehicle. If we change this to protected, we will 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 super class as public, then the methods in the subclass must also be public, otherwise you will get this error. But the following is true. If you make the method's 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 will still work, but if we make it private it will not work in this case, 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.

Okay, so after mentioning this important point, we can now end our lesson. I hope that you understand the method overriding subject clearly. So, I'll see you in the next lesson my friends.


About the Author
Learning Paths

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.