Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconVirtual Function in Java

Virtual Function in Java

Last updated:
14th Jun, 2023
Views
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
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. 

Java is well-known in the field of object-oriented programming for its powerful features and adaptability. The execution of virtual functions is one of the major features that distinguishes Java. Now, let’s understand what is virtual function in Java. Virtual functions provide flexibility in method binding, allowing for polymorphism and increasing code reusability. This blog will go further into the realm of Java virtual functions, investigating their intended purpose, implementation, and the intricacies of pure virtual functions.

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

Explore our Popular Software Engineering Courses

Ads of upGrad blog

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. 

Method Overriding

A more extensive description of method overriding and how it applies to virtual functions in Java is an essential addition. Method overriding is the technique of giving a distinct way to implement a method specified in the base class in the derived class. The derived class can then modify or enhance the operation of the inherited method.

When an approach in the base class is labeled “virtual,” it means it can be overridden in the derived class. When the technique is called via a derived class reference, the derived class’s representation of the virtual method will be triggered rather than the base class’s implementation. A major aspect of virtual functions in Java is the dynamic connection of the method call to the suitable implementation during execution.

In order to override a virtual function in Java, the derived class’s technique needs to have the exact same name, return type, and parameters list as the virtual function in the base class. It should be noted that the ‘@Override’ annotation can be utilized to guarantee that the planned overriding is done appropriately. This annotation performs compile-time checks and aids in avoiding the possibility of problems when overriding methods.

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.

Interfaces in Java may also be utilized for defining virtual functions. Any class that defines an interface can specify actions that must be executed. These approaches can be considered virtual functions since they create a contract that implementing classes must follow.

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.

A pure virtual function in Java is strongly connected to abstract classes in Java. An abstract class is a non-instantiable class that could describe abstract methods, which are basically pure virtual functions. Any concrete (non-abstract) subclass must override an abstract method specified without implementation.

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.

Ads of upGrad blog

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. 

Summary:

  • 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!

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.

Explore Free Courses

Suggested Blogs

OnClick Function in JQuery Explained with Examples
101634
One of the fast, small, and feature-rich libraries in JavaScript is jQuery. jQuery is used for making HTML documents, event handling, traversal, and m
Read More

by Rohan Vats

09 Feb 2024

Average Computer Science Salary in India [For Freshers & Experienced]
901264
Computer science is among the most popular sectors in India. Thanks to the various career opportunities this field offers, there’s a lot of demand for
Read More

by Rohan Vats

04 Feb 2024

Software Process & Software Process Models [Types of Software Process Models]
32808
The term software refers to a specially formulated set of computer programs the associated documents, and the processes that elaborate software progra
Read More

by Rohan Vats

04 Feb 2024

25 Exciting Best Django Project Ideas & Topics For Beginners [2023]
135443
What is a Django Project? Django projects with source code are a collection of configurations and files that help with the development of website app
Read More

by Kechit Goyal

28 Jan 2024

Top 5 Java Web Application Technologies You Should Master in 2023
49195
Web Development in Java Java is a commonly used language for web development, especially on the server-side. Java web applications are distributed ap
Read More

by Rohan Vats

28 Jan 2024

12 Interesting Computer Science Project Ideas & Topics For Beginners [Latest 2024]
148086
Summary: In this article, you will learn 12 Interesting Computer Science Project Ideas & Topics For Beginners (2024). Face detection Online auct
Read More

by Pavan Vadapalli

24 Jan 2024

DevOps Engineer Salary in India in 2024 [For Freshers & Experienced]
905104
Wondering what is the range of DevOps Salary in India? In the last few years, DevOps has evolved from being just a “buzzword” to a mainstream practic
Read More

by upGrad

24 Jan 2024

Top 15 Full Stack Developer Frameworks to learn in 2024
6267
Introduction to Full Stack Developer Frameworks Full-stack developer frameworks refer to a set of tools, libraries, and technologies that developers
Read More

by Pavan Vadapalli

23 Jan 2024

Yarn vs NPM: Which Package Monitor to Choose
5403
Introduction to Package Monitors Two of the most renowned package managers among Node.js and JavaScript developers are NPM and Yarn. Implementing eit
Read More

by Pavan Vadapalli

23 Jan 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon