Table of Contents
Now, in both the subclasses, the “material” method will be used, and with polymorphism, it is possible to use the same method “material” of parent class in both the subclass.
More like this:
- Get the Average of Two Numbers in Java
- How to Get Operating System Name using Java
- Copy File Content to Another Using Java
In Java, there are two types of Polymorphism available.
- Static Polymorphism
- Dynamic Polymorphism
In this tutorial, you’ll learn about both of these with appropriate illustrations.
Static Polymorphism in Java
The compiler detects static Polymorphism in compile-time, and all the steps to perform this polymorphism are prepared by the compiler. And before proceeding with the run-time process, the compiler prepares the system.
Method overloading and Operator overloading are two ways to achieve static polymorphism in the program.
For instance:
class Adddemo { // Addition Method with 2 parameters static int Addition(int x, int y) { return x + y; } //Another Addition Method with the same name but it has 3 parameters static int Addition(int x, int y, int z) { return x + y + z; } } class Main { public static void main(String[] args) { Adddemo ob = new Adddemo(); ob.Addition(5, 9)); ob.Addition(5, 7, 2)); } }
Output:
14 14
Dynamic Polymorphism in Java
[adinserter block=”2″] Dynamic polymorphism occurs at the run time, and JVM handles all the steps required to make it happen. JVM takes all necessary decisions, and the compiler is unaware of this polymorphism.Method overriding is a way to achieve dynamic polymorphism. In dynamic polymorphism, we need two classes (parent and base both).
For instance:
class Parentclass { void display() { System.out.println("This is the parent class"); } } class childclass extends Parentclass { void display() { System.out.println("This is Child class"); } } class secondchildclass extends Parentclass { void display() { System.out.println("This is secondsubclass"); } } class Mainclass { public static void main(String[] args) { Parentclass ob; ob = new childclass(); ob.display(); ob = new secondchildclass(); ob.display(); } }
If you notice the above program, the display method is used in both the child classes and parent classes. But each of these has a different body. With the ob instance of the parent class, the display method of child classes was called.
This is what polymorphism means, using the same method or element in multiple ways.
Advantages of Polymorphism in Java
- Reduces Line of Code
- Makes debugging easy
- Execution time is reduced
- Code reusability is increased
- Programs become more readable
- Space complexity is reduced
Conclusion
For a beginner, polymorphism is tricky to understand, but after a few practice programs, you’ll find it extremely helpful to implement complex programs with different interrelated classes. Polymorphism plays a vital role in interlinked classes. It makes the whole handling of data easy for the programmer; besides, it becomes easy to read and understand. Polymorphism might not seem important right now, but you will find it highly efficient when implementing tricky programs.
Blog Hero