Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

Is it possible to write virtual functions/ methods in Java, as we do in C++?

1 Answer

0 votes
by (13.1k points)

Yes, we have virtual functions in Java. All the instance methods in Java are virtual by default. However, some methods are not virtual:

  • Class methods
  • Private instance methods

Here are some examples:

“Normal” virtual functions

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 Fish());

      animals.add(new Goldfish());

      animals.add(new OtherAnimal());

      for (Animal currentAnimal : animals) 

      {

         currentAnimal.eat();

      }

   }

}

class Fish extends Animal 

{

   @Override

   public void eat() 

   { 

      System.out.println("I eat like a fish!"); 

   }

}

class Goldfish extends Fish 

{

   @Override

   public void eat() 

   { 

      System.out.println("I eat like a goldfish!"); 

   }

}

class OtherAnimal extends Animal {}

Output:

I eat like a generic Animal.

I eat like a fish!

I eat like a goldfish!

I eat like a generic Animal.

Example with virtual functions with interfaces:

In Java, interface methods are all virtual. They must be virtual because they rely on the implementing classes to provide method implementation. The code to execute will only be selected at run time:

For example:

interface Bicycle {         //the function applyBrakes() is virtual because

    void applyBrakes();     //functions in interfaces are designed to be 

}                           //overridden.

class HeroBicycle implements Bicycle {

    public void applyBrakes(){               //Here we implement applyBrakes()

       System.out.println("Brakes applied"); //function

    }

}

Example with virtual functions with abstract classes.

Abstract classes must contain virtual methods because they rely on extending classes’ implementation. For Example:

abstract class Dog {                   

    final void bark() {               //bark() is not virtual because it is 

        System.out.println("woof");   //final and if you tried to override it

    }                                 //you would get a compile time error.

    abstract void jump();             //jump() is a "pure" virtual function 

}                                     

class MyDog extends Dog{

    void jump(){

        System.out.println("boing");    //here jump() is being overridden

    }                                  

}

public class Runner {

    public static void main(String[] args) {

        Dog dog = new MyDog();       // Create a MyDog and assign to plain Dog variable

        dog.jump();                  // calling the virtual function.

                                     // MyDog.jump() will be executed 

                                     // although the variable is just a plain Dog.

    }

}

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 15, 2021 in Java by Jake (7k points)

Browse Categories

...