A virtual function is a method that supports runtime polymorphism by allowing a subclass to provide its own implementation of an inherited method. In Java, this behavior is achieved through method overriding and dynamic method dispatch rather than a dedicated virtual keyword.
In this chapter, you will learn about virtual functions in Java, how they work, and how they support runtime polymorphism.
A virtual function, also called a virtual method, is a method whose implementation is determined at runtime based on the actual type of the object. Java supports this behavior through method overriding.
Unlike C++, which uses the virtual keyword to declare virtual functions, Java does not require a special keyword for virtual methods. Most instance methods are dynamically dispatched, except methods that are static, private, or final.
Virtual functions are closely related to runtime polymorphism in Java and inheritance in Java. These concepts allow subclasses to provide specialized implementations of methods inherited from their parent classes.
When a subclass overrides an instance method, Java determines which implementation to execute at runtime based on the actual object type rather than the reference variable's declared type. This behavior is known as dynamic method dispatch.
Static methods are resolved based on the reference type, private methods are not overridden, and final methods cannot be overridden. Therefore, these methods do not participate in runtime polymorphism through method overriding.
For more information, see Virtual Function in C++.
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:
Parent.Java:
Child.java:
Output:
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.
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:
Output:
BMW X7
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.
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:
Output:
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:
Output:
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:
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India