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 Tutorials

View All

Suggested Blogs

Top 14 Technical Courses to Get a Job in IT Field in India [2024]
90952
In this Article, you will learn about top 14 technical courses to get a job in IT. Software Development Data Science Machine Learning Blockchain Mana
Read More

by upGrad

15 Jul 2024

25 Best Django Project Ideas & Topics For Beginners [2024]
143863
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

11 Jul 2024

Must Read 50 OOPs Interview Questions & Answers For Freshers & Experienced [2024]
124781
Attending a programming interview and wondering what are all the OOP interview questions and discussions you will go through? Before attending an inte
Read More

by Rohan Vats

04 Jul 2024

Understanding Exception Hierarchy in Java Explained
16874
The term ‘Exception’ is short for “exceptional event.” In Java, an Exception is essentially an event that occurs during the ex
Read More

by Pavan Vadapalli

04 Jul 2024

33 Best Computer Science Project Ideas & Topics For Beginners [Latest 2024]
198249
Summary: In this article, you will learn 33 Interesting Computer Science Project Ideas & Topics For Beginners (2024). Best Computer Science Proje
Read More

by Pavan Vadapalli

03 Jul 2024

Loose Coupling vs Tight Coupling in Java: Difference Between Loose Coupling & Tight Coupling
65177
In this article, I aim to provide a profound understanding of coupling in Java, shedding light on its various types through real-world examples, inclu
Read More

by Rohan Vats

02 Jul 2024

Top 58 Coding Interview Questions & Answers 2024 [For Freshers & Experienced]
44554
In coding interviews, a solid understanding of fundamental data structures like arrays, binary trees, hash tables, and linked lists is crucial. Combin
Read More

by Sriram

26 Jun 2024

Top 10 Features & Characteristics of Cloud Computing in 2024
16289
Cloud computing has become very popular these days. Businesses are expanding worldwide as they heavily rely on data. Cloud computing is the only solut
Read More

by Pavan Vadapalli

24 Jun 2024

Top 10 Interesting Engineering Projects Ideas & Topics in 2024
43094
Greetings, fellow engineers! As someone deeply immersed in the world of innovation and problem-solving, I’m excited to share some captivating en
Read More

by Rohit Sharma

13 Jun 2024

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