Virtual Function in Java
A virtual function or virtual method in an OOP language is a function or method used to override the behavior of the function in an inherited class with the same signature to achieve the polymorphism.
When the programmers switch the technology from C++ to Java, they think about where is the virtual function in Java. In C++, the virtual function is defined using the virtual keyword, but in Java, it is achieved using different techniques. See Virtual function in C++.
By default, all the instance methods in Java are considered as the Virtual function except final, static, and private methods as these methods can be used to achieve polymorphism.
How to use Virtual function in Java
The virtual keyword is not used in Java to define the virtual function; instead, the virtual functions and methods are achieved using the following techniques:
In a nutshell, the methods or functions that can be used to achieve the polymorphism are the virtual functions or methods in Java.
Let's understand it with some examples:
Inside the Child Class
From the above example, we have used the function v1() as the virtual function by overriding it in the Child class.
In Java Every non-static, non-final, and public method is a virtual function. These methods can be used to achieve polymorphism. The methods that can not be used to achieve the polymorphism never be virtual function.
A static, final, and private method never be the virtual function. We can not call the static method by using the object name or class name. Even if we try, it will not be able to achieve the polymorphism.
Java Interfaces as Virtual Function
An interface in Java is a blueprint of a class; it holds static constants and abstract methods. All Java Interfaces are considered virtual functions, as they depend on the implementing classes to provide the method implementation.
Consider the below example to understand the behavior of the interface:
From the above example, we can see the interface's method is executed using the implementing class BMW.
Hence, we can also achieve polymorphism using the Interfaces.
Pure Virtual Function
A virtual function for which we are not required implementation is considered as pure virtual function. For example, Abstract method in Java is a pure virtual function. Consider the below example:
MyPet is so sweet
From the above example, the jump() method is a pure virtual function.
Runtime polymorphism is a process in which a call to an overridden method is resolved at runtime rather than compile time.
In runtime polymorphism, we will use the reference variable to call a method instead of a reference variable.
The virtual functions can be used to achieve runtime polymorphism.
Consider the below example to understand how the virtual function is used to achieve the runtime polymorphism:
Best Java Training Institute
From the above example, we have achieved runtime polymorphism using the virtual function.
Below are some consideration points about the Virtual function in Java: