Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Virtual Function in Java

A virtual function or virtual method in an OOP language is a function or method used to override the behavior of the function in an inherited
class with the same signature to achieve the polymorphism.

When the programmers switch the technology from C++ to Java, they think about where is the virtual function in Java. In C++, the virtual
function is defined using the virtual keyword, but in Java, it is achieved using different techniques. See Virtual function in C++.

Java is an object-oriented programming language; it supports OOPs features such as polymorphism, abstraction, inheritance, etc. These
concepts are based on objects, classes, and member functions.

By default, all the instance methods in Java are considered as the Virtual function except final, static, and private methods as these methods
can be used to achieve polymorphism.

History of Java

How to use Virtual function in Java

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:

o We can override the virtual function with the inheriting class function using the same function name. Generally, the virtual function
is defined in the parent class and override it in the inherited class.
o The virtual function is supposed to be defined in the derived class. We can call it by referring to the derived class's object using the
reference or pointer of the base class.
o A virtual function should have the same name and parameters in the base and derived class.
o For the virtual function, an IS-A relationship is necessary, which is used to define the class hierarchy in inheritance.
o The Virtual function cannot be private, as the private functions cannot be overridden.
o A virtual function or method also cannot be final, as the final methods also cannot be overridden.
o Static functions are also cannot be overridden; so, a virtual function should not be static.
o By default, Every non-static method in Java is a virtual function.
o The virtual functions can be used to achieve oops concepts like runtime polymorphism.

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:

1.   class Parent {  
2. void v1() //Declaring function  
3. {  
4. System.out.println("Inside the Parent Class");  
5. }  
6. }  

Child.java:

1. public class Child extends Parent{  
2.             void v1() // Overriding function from the Parent class  
3.             {  
4.             System.out.println("Inside the Child Class");  
5.             }  
6.             public static void main(String args[]){  
7.             Parent ob1 = new Child(); //Refering the child class object using the parent class  
8.             ob1.v1();  
9.             }  
10.             }  

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.

Java Interfaces as Virtual Function

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:

1. interface Car{    
2. void print();    
3. }    
4. class BMW implements Car{    
5. public void print(){System.out.println("BMW X7");}    
6. public static void main(String args[]){    
7. BMW obj = new BMW();    
8. obj.print();    
9.  }    
10. }    

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.

    

Pure Virtual Function

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:

1. abstract class Animal {  
2.         final void bark(){  
3.         System.out.println("Dog");  
4.         }  
5.         abstract void jump(); // Abstract Method (Pure Virtual Function)  
6. }  
7.     class MyPet extends Animal{  
8.         void jump(){  
9.         System.out.println("MyPet is so sweet");  
10.         }  
11. }  
12. public class Demo {  
13.         public static void main(String args[]){  
14.         Animal ob1 = new MyPet();  
15.         ob1.jump();  
16.         }  
17.         }  

Output:

MyPet is so sweet

From the above example, the jump() method is a pure virtual function.

      

Runtime Polymorphism

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:

1. class Javatpoint{  
2. public void show() //Virtual Function  
3. {  
4. System.out.println("welcome to JavaTpoint");  
5. }  
6. }  
7. class Training extends Javatpoint{  
8. public void show(){  
9. System.out.println("Best Java Training Institute");  
10. }  
11. public static void main(String args[]){  
12. Javatpoint ob1 = new Training();  
13. ob1.show();  
14. }}  

Output:

Best Java Training Institute

From the above example, we have achieved runtime polymorphism using the virtual function.
Summary:

Below are some consideration points about the Virtual function in Java:

o The Virtual Function is an ordinary function in Java.


o We are not required to declare any explicit description to define the virtual function.
o In Java, no virtual keyword is used to define the virtual function.
o The Parent class pointer is used to refer to the object of the child class
o the virtual function should be defined in the child class with the same name in the parent class.
o All the instance methods in Java are considered the Virtual function except final, static, and private methods.

You might also like