Polymorphism

Start course
Overview
Difficulty
Beginner
Duration
2h 28m
Students
13
Description

In this course, we'll learn about object-oriented programming and its implementations.

Learning Objectives

  • Inheritance
  • Method Overriding
  • Super and This Keywords
  • Final Keyword
  • Abstract Classes
  • Interfaces
  • Polymorphism 
  • Encapsulation

Intended Audience

  • 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

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll talk about the implicit casting or in other words, Polymorphism in Java. In the inheritance video, we touched it a little bit on the slide. 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 and it has the accelerate method, and we have two subclasses that override the accelerate method of the superclass Vehicle. Accelerate works for any type of vehicle, not only a car but also a motorcycle. Accelerate action is done in a 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, reference variable vehicle, declared of type vehicle is pointing to an object of Car class. Same way, the Vehicle vehicle1 = new Motorcycle(); statement works because the motorcycle is a subclass of the vehicle too. Here, reference variable vehicle1 declared of type vehicle is pointing to an object of Motorcycle class. This feature of Java is known as polymorphism. 

We implemented static polymorphism that includes method overloading in the method section. So, we'll implement dynamic polymorphism that includes method overriding in this exercise. Let's make some examples to understand polymorphism. In the object-oriented programming project, right click on the source folder and select new class. I'll specify the package name as polymorphism and the class name is Vehicle. And click the 'Finish" button. The Vehicle class will be our superclass. 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. Okay, let's create a new subclass. Right click on the same package and select new class. I'll specify the class name as Car and click 'Finish'. Our subclass Car will extend the superclass Vehicle. 

This means that subclass Car inherits the accelerate method from the superclass Vehicle. Let's override this method. We can start with override annotation but it is not mandatory. In this method, I will print the "Car is accelerating..." message and I will save the code. Okay, let's create a new subclass. Right click on the same package and select new class. I'll 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, first, I'll 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 "Car is accelerating..." message, not the "Vehicle is accelerating..." because the vehicle object pointer the Car class. Okay, let's run the code. You 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'll 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 achieve this error, we should do the explicit casting here. So, before the vehicle object, I write (car) in the parentheses. As you can see, the error has disappeared. This 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 that. if(vehicle instanceof Car), you can assign the vehicle to myCar, with the 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. Okay, now let's create another reference object of the Vehicle class by pointing Motorcycle class. Vehicle vehicle1 = 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. You see in the console, the "Motorcycle is accelerating" message. Okay, this was the polymorphism in Java. Let's take a short break here. See you in the next video.

 

About the Author
Students
72
Courses
18
Learning Paths
1

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.

Covered Topics