image
Polymorphism
Start course
Difficulty
Beginner
Duration
1h 46m
Students
22
Ratings
5/5
Description

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

Prerequisites

  • Basic Java knowledge
Transcript

Hello there, my friends.

In this video we will discuss Implicit Casting, also known as Polymorphism in Java. We touched on it briefly on the slide in the inheritance video. Polymorphism is used to perform a single action in different forms. In Java, we have two types of polymorphism. The first one is static and the second one is dynamic. Method overloading is an example of static polymorphism. Method overriding is an example of dynamic polymorphism. On the slide, you see dynamic polymorphism. For example, we have a vehicle superclass with the accelerate method and we have two subclasses that override the accelerate method of a super class vehicle. Accelerate works for any type of vehicle, not only a car but also a motorcycle. Accelerate action is done in different form in car and motorcycle classes when we create an object of the subclass and assign it to the superclass reference. If we call the overridden accelerate method on the superclass vehicle reference, then the overridden method of subclass will be called.

If we want to create two objects, vehicle vehicle = new Car () statement works, because car is a subclass of vehicle. Here, the reference variable vehicle declared of type vehicle is pointing to an object of car class. In the same way, the vehicle vehicle1 = new Motorcycle () statement works because the motorcycle is a subclass of the vehicle2. Here the reference variable vehicle 1 declared of type vehicle is pointing to an object of motorcycle class. This feature of Java is known as polymorphism. So, we will implement dynamic polymorphism that includes method overriding in this exercise. Let's try to understand polymorphism better with the help of some examples. In the object-oriented programming project, right-click on the 'Source' folder and select new class. I will specify the package name as polymorphism and the class name is Vehicle. And click the 'Finish' button.

The vehicle class will be our super class. Let's declare a public void method named accelerate in this class. We print the "Vehicle is accelerating" message by using the print() method and I will save the code. All right, let's create a new subclass. Right-click on the same package and select new class. I will specify the class name as Car and click 'Finish'. Our subclass car will extend the super class vehicle. This means that subclass car inherits the accelerate method from the super class vehicle. All right, let's override this method. We can start with override annotation, but it's not mandatory. In this method, I will print the "Car is accelerating" message and I will save the code. All right, let's create a new subclass. Right-click on the same package and select new class. I will specify the class name as Motorcycle. Our subclass motorcycle will extend superclass vehicle. We write the accelerate method to override by adding the override annotation. In this method, we print the "Motorcycle is accelerating" message, and I will save the code again.

Let's create the main test class named Test in the same package. In the main method, firstly, I will create a vehicle object reference by pointing to car class. Vehicle vehicle = new Car(). This code works because the car class is a subclass of the vehicle. Class vehicle is extended by class car and its method accelerate is overridden in class car. In this definition, the reference variable vehicle is pointing to an object of the car class. If we call the accelerate method, it will print the cars accelerating message, not the vehicle is accelerating, because the vehicle object points to the car class. Got it? All right, let's run the code. Okay as you can see in the console, 'Car is accelerating' message is displayed. Okay, but if we create an object from the car class and assign the vehicle object to the object of the car class, we get a compilation error. For example, let's create a car object. Car myCar =, after the equal sign I will write the vehicle object. And as you can see, we get a compilation error. It says Type mismatch: cannot convert from Vehicle to Car because the vehicle is the object of the vehicle class, it simply points to the car class, but is actually an object of the vehicle class. To solve this error, we should do explicit casting here.

So, before the vehicle object, I write car in the parentheses. As you can see the error has disappeared. The cast inserts a runtime check that vehicle is assigned a car, so that the compiler can safely assume that vehicle is a car. If the vehicle is not a car at runtime, an exception will be thrown. In this case, you can make a logical test as to the type of particular object using the instance of operator. This can save you from a runtime error owing to an improper cast. For example, you can create an if statement like this. If vehicle is an instance of the car, you can assign the vehicle to myCar with explicit casting. Here, the instance of operator verifies that vehicle refers to a car, so that we can make the cast with the knowledge that there will be no runtime exception thrown. Got it? Now, let's create another reference object of the vehicle class by pointing to the motorcycle class. Vehicle vehicle 1 = new Motorcycle. In this definition, the reference variable vehicle1 is pointing to an object of the motorcycle class. If we call the accelerate method, it will print the 'motorcycle is accelerating' message. Okay, let's run the code again. So, you can see in the console the 'Motorcycle is accelerating' message. And that is polymorphism in Java. Let's take a short break here my friends and I'll see you in the next video.

 

About the Author
Students
3900
Courses
64
Learning Paths
5

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.