The ability of an object to take many forms.
We can create polymorphic objects when we have a parent-child relationship between classes or interfaces and their implementations.
Let’s say we have these classes
We can create objects like this
Car car = new BMW();// orCar car2 = new Audi();
So you can see how Car object can be BMW and Audi as well. That’s why we say the ability of an object to take many forms.
Car car =
This part is a reference type. We always need to remember that reference type will decide what methods and properties available for the object.
= new BMW();
= new Audi();
This is the actual object part. If BMW or Audi classes override the methods from the Car class. During the runtime, the overridden method will get called.
The polymorphism with interfaces and abstract classes will work exactly the same.
public class Car {
public String model;
public double sdeed;
public void drive() {
System.out.println("Car is driving");
} public void stop() {
System.out.println("Car is stopping");
}}
and let’s create a BMW class.
public class BMW extends Car {
@Override
public void drive() {
System.out.println("BMW is driving");
} public void driveFast() {
System.out.println("BMW is driving fast");
}
}
and the way we use
public class Main {
public static void main(String[] args) {
Car car = new BMW(); car.drive(); // BMW is driving
car.stop(); // Car is stopping // car.driveFast(); does not compile
}
}
car.drive();
will printBMW is driving
because the BMW class overrides the parent method. This is also known as runtime polymorphism because java thinks it will execute the method of the Car class but during the runtime, it will figure out that BMW overrides it and will execute BMW’s method.car.stop();
the output will beCar is stopping
the parent class method will get called.car.driveFast();
will not compile because. The reference type decides what methods are available forcar
the object. AndCar
does not havedriveFast();
method.
That’s it for polymorphism in Java. Have a nice day!
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET