Virtual Function in JAVA

The programmers coming from c++ background to Java normally think that where is the Virtual function?  In Java there is no keyword names “virtual“.

Definition of Virtual from wiki:

In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature to provide the polymorphic behavior.

Therefore according to definition, every non-static method in JAVA is by default virtual method except final and private methods. The methods which cannot be inherited for polymorphic behavior is not a virtual method.


import java.util.*;

public class Animal
{
   public void eat()
   {
      System.out.println("I eat like a generic Animal.");
   }

   public static void main(String[] args)
   {
      List<Animal> animals = new LinkedList<Animal>();

      animals.add(new Animal());
      animals.add(new Wolf());
      animals.add(new Fish());
      animals.add(new Goldfish());
      animals.add(new OtherAnimal());

      for (Animal currentAnimal : animals)
      {
         currentAnimal.eat();
      }
   }
}

public class Wolf extends Animal
{
   @Override
   public void eat()
   {
      System.out.println("I eat like a wolf!");
   }
}

public class Fish extends Animal
{
   @Override
   public void eat()
   {
      System.out.println("I eat like a fish!");
   }
}

public class Goldfish extends Fish
{
   @Override
   public void eat()
   {
      System.out.println("I eat like a goldfish!");
   }
}

public class OtherAnimal extends Animal {}

Output:

I eat like a generic Animal.
I eat like a wolf!
I eat like a fish!
I eat like a goldfish!
I eat like a generic Animal.

Abstract class is nothing but the pure virtual method equivalent to C++ in Java.


Question : why we say that static method is not a virtual method in Java?

Answer : static method is bound to the class itself, so calling the static method from class name or object does not provide the polymorphic behavior to the static method. We can override the static method however it will not give the advantage of the polymorphism.

Posted

in

by

Tags:


Related Posts

Comments

15 responses to “Virtual Function in JAVA”

  1. […] java. but except static, final and private method all the methods are virtual bu default.  Refer this article to read more on […]

  2. Madhu Avatar
    Madhu

    Great article, it kinda refreshed what virtual functions are in C++ for me.

    1. Jitendra Zaa Avatar

      Thanks Madhu

  3. sreenath Avatar
    sreenath

    we know because of diamond problem there is no multiple inheritance in java. why java is not solving diamond problem using virtual keyword like in c++.

    1. Vinod Suthersan Avatar

      Since there is no possibility of a diamond problem in java.. Java doesn’t know that there is something called as the diamond problem existing. There is multiple inheritance in Java… what are Interfaces for?

      1. Bliss Terra Avatar
        Bliss Terra

        Java does not have multiple inheritance. The ability to implement multiple interfaces in a class does give us the illusion that what we do is multiple inheritance, but in fact the implementation of those interfaces is singly inherited. Because of this, the compiler will not get confused by the overridden methods of the upper classes. And so, the `diamond problem` does not exist.

      2. Davide Cannizzo Avatar

        You inherit classes, but implement interface. They are two different things: each interface acts as a contract that restricts you to implement (declare) all those members the interface declares in the class which implements it. Whereas, inheriting a class means that the instance of the super class is created each time it is for the subclass, just like at least one of its constructors is called before one of the subclass is. Your subclass is a super class with something more. Instead, implementing an interface is just making different class similar for some aspect. For example, if you have a mathematical function to evaluate and you can use different algorithms to perform for it, you could think to declare a class for each algorithm, implementing an interface that unifies them for the fact they’re attempting to get to the same goal.

  4. vinu Avatar
    vinu

    Great post. Explained well. Helped me a lot. Thanks Bro

  5. Ayesha Avatar
    Ayesha

    Explain the concept of polymorphism please !

    1. Davide Cannizzo Avatar

      Polymorphism regards the ability to declare more members with the same name. In many languages, such as Java and even C#, you can get a polymorphism for methods in two ways:
      – Overriding those methods, so that you’re re-declaring a method in the subclass, when it was already written in the super class.
      – Overloading a method. That is to say that you’re naming another method the same. To do such a thing, you need to distinct that method by the others by making its parameters different for their types or count.

  6. Sidd Avatar
    Sidd

    every non-static method in JAVA is by default virtual method except final and private methods. How all the methods are virtual , I think only overriden methods are virtual.

    1. Davide Cannizzo Avatar

      virtual indicates the capability of a method to be overridden. No matter for whether the method is overridden. Theorically, an overridable class can also not to be overridden, and if you don’t subclass any class, you won’t be able to override any method. However, if those method is private or virtual, you won’t be able to even override them. This is what ‘virtual’ means.

  7. Varghese Avatar
    Varghese

    @Sidd

    Here virtual just means we can override functions. Its up to you whether you are overriding it or not.

  8. Anil Avatar
    Anil

    Nice explination

  9. mobin Avatar
    mobin

    Great, Thank you.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading