Virtual Function in Java

A method or a function in Java refers to a group of statements that perform a particular task and revert an appropriate result. The Java functions sometimes perform tasks without returning any value or output to the caller. The use of functions eliminates the need of scripting the same code again and again. Instead, the code statements can be reused as many times as required. Unlike Python, C and C++, the functions in Java must be a part of one or the other class. 

Check out our free technology courses to get an edge over the competition.

Explore our Popular Software Engineering Courses

Basics of Virtual function in Java:

A method or a function that overrides the behavior of an inherited class function with the identical signature to accomplish polymorphism is called the virtual method or virtual function in an object-oriented programming language. Unlike the use of the keyword ‘virtual’ to use virtual functions in C++, Java has several techniques through which the use of a virtual function can be accomplished. 

All the Java instance methods are treated as virtual functions except for the static, final and private methods. The private, static and final methods are used to accomplish polymorphism and hence are not regarded as virtual functions. 

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

In-Demand Software Development Skills

How can a virtual function be used in Java?

Java Virtual Functions are not defined by the keyword ‘virtual’. However, there are several other techniques that can enable the use of virtual functions in Java. Some important features of the virtual functions in Java are listed below. 

  • An inheriting class function can override a virtual function with the same name and input parameters. In general, virtual functions that are defined in the parent class are overridden in the derived class. 
  • Virtual functions in Java must be defined in the child class. In this case, the virtual function can be called by stating the object in the child class using the pointer or reference of the base class. 
  •  The name and arguments of the virtual function must be identical to that of the base and the derived class. 
  • An IS-A relationship is required for the virtual function in Java that is used to define the hierarchy of a class in inheritance. 
  • Private functions do not permit overriding and hence, the virtual functions are not private functions. 
  • Similarly, function final can also not be overridden and hence the virtual function is not the final. 
  • Static functions in Java also do not allow overriding and hence virtual functions are not static. 
  • All the non-static functions in Java are virtual functions.
  • Virtual functions are employed in achieving runtime polymorphism. Hence, virtual functions can also be defined as the functions that are used to accomplish polymorphism in Java. 

Consider the example of a Java program shown below to understand the concept of the virtual function.

In the above example, the function v1() is used as the virtual function as it is overridden in the Child class.

Virtual Function and Java Interfaces:

A Java interface is a blueprint or a template of any class in the Java program. The interfaces embrace the abstract methods and static constants in Java. As the Java interfaces are dependent on the executing classes to facilitate the implementation of a method, they are considered virtual functions. Let us consider an example of the code below to understand the functioning of the Java interface. 

In the above example, the execution of a method of interface is accomplished using BMW, the implementing class. Hence, interfaces can also be used to achieve polymorphism. 

Read our Popular Articles related to Software Development

Pure Virtual Function in Java:

A virtual function that does not require an implementation is regarded as a pure virtual function. An abstract method is a pure virtual function in Java. Consider the example shown below to understand the operation of a pure virtual function. In this example, the pure virtual function used is jump().

Runtime Polymorphism in Java:

The process that involves the resolution of a call to an overridden method that happens at the runtime instead of taking place at the compile time is called the runtime polymorphism. This can be accomplished by using virtual functions in Java. The example below reflects the use of a virtual function to achieve runtime polymorphism.

How is a virtual function different from a pure virtual function in Java?

Virtual functions in Java are defined in a class, whereas pure virtual functions are defined nowhere. The syntax used to declare a virtual function and a pure virtual function are virtual funct_name(parameter_list){….}; and virtual funct_name(parameter_list)=0; respectively. There is no concept of derived class in virtual functions. However, if, in case, a class consists of at least one pure virtual function, it is declared as abstract. A virtual function can be overridden by a base class only if necessary. But, in pure virtual functions, the derived class must override the pure virtual function for sure. 


  • The virtual function is the same as any other ordinary function in Java.
  • No explicit description is to be declared for defining a virtual function in Java. 
  • The keyword ‘virtual’ is not used in defining a virtual function in Java.
  • In the virtual function, the pointer of the parent class is used to refer to an object in the child class. 
  • The virtual function should have the same name and signature in both parent and child classes.
  • Except for final, private and static methods, all other instance methods are regarded as the Virtual functions in Java.
  • Virtual functions must be capable of implementing polymorphism in Java. In other words, any function that does not achieve polymorphism can never be a virtual function.

Amid increased conversations around crypto and Blockchain technology, if you wish to educate yourself professionally on the topic, then upGrad’s Executive Post Graduate Programme in Software Development – Specialisation in Blockchain under IIIT- Bangalore is the right choice for you!

Want to share this article?

Plan your Software Development Career Now!

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks