What Is A Virtual Function In Java?

Last updated on Jun 10,2021 33.3K Views

What Is A Virtual Function In Java?

edureka.co

Java is an object-oriented programming language which supports concepts like polymorphism, inheritance, abstraction, etc. These OOPs concepts revolve around classes, objects, and member functions. The virtual function is one such concept which helps in run-time polymorphism. In this blog, we will learn about virtual function in java. The following topics are discussed in this article.

What is a Virtual Function In Java?

The behavior of a virtual function can be overridden with the inheriting class function with the same name. It is basically defined in the base class and overridden in the inherited class.

Virtual function in Java is expected to be defined in the derived class. We can call the virtual function by referring to the object of the derived class using the reference or pointer of the base class.

Every non-static method in Java is by default a virtual method. Java does not have a virtual keyword like C++, but we can define them and use them for concepts like run-time polymorphism.

Virtual Function Example

Let’s take a look at an example to understand how we can use virtual functions in Java.

class Vehicle{
void make(){
System.out.println("heavy duty");
}
}
public class Trucks extends Vehicle{
void make(){
System.out.println("Transport vehicle for heavy duty");
}
public static void main(String args[]){
Vehicle ob1 = new Trucks();
ob1.make();
}
}
Output: Transport vehicle for heavy duty

Every non-static method in Java is a virtual function except for final and private methods. The methods that cannot be used for the polymorphism is not considered as a virtual function.

A static function is not considered a virtual function because a static method is bound to the class itself. So we cannot call the static method from object name or class for polymorphism. Even when we override the static method it does not resonate with the concept of polymorphism.

Virtual Function With Interfaces

All Java interfaces are virtual, they rely on the implementing classes to provide method implementations. The code for execution is selected at run-time. Here is a simple example for better understanding.

interface Car{
void applyBrakes();
}
interface Audi implements Car{
void applyBrakes(){
System.out.println("breaks Applied");
}
}

Here applyBreaks() is virtual because functions in interfaces are designed to be overridden.

Pure Virtual Function

Pure virtual function is a virtual function for which we don’t have implementations. An abstract method in Java can be considered as a pure virtual function. Let’s take an example to understand this better.

abstract class Dog{
final void bark(){
System.out.println("woof");
}
abstract void jump(); //this is a pure virtual function
}
class MyDog extends Dog{
void jump(){
System.out.println("Jumps in the air");
}
}
public class Runner{
public static void main(String args[]){
Dog ob1 = new MyDog();
ob1.jump();
}
}

Output: Jumps in the air

This is how virtual function can be used with abstract class.

Run-Time Polymorphism

Run-time polymorphism is when a call to an overridden method is resolved at run-time instead of compile-time. The overridden method is called through the reference variable of the base class.

class Edureka{
public void show(){
System.out.println("welcome to edureka");
}
}
class Course extends Edureka{
public void show(){
System.out.println("Java Certification Program");
}
public static void main(String args[]){
Edureka ob1 = new Course();
ob1.show();
}
}
Output: Java Certification Course

Points To Remember

This brings us to the end of this article where we have learned about the Virtual Function In Java. I hope you are clear with all that has been shared with you in this tutorial.

If you found this article on “Virtual Function In Java” relevant, check out the Edureka’s Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Virtual Function In Java” and our team will be glad to answer.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework