c-plus-plus
  1. c-plus-plus-default-virtual-behaviour-in-c-and-java

Default Virtual Behaviour in C++ and JAVA

Virtual functions in C++ and JAVA are used to achieve polymorphism. They allow a subclass to provide its own implementation of a method defined in the parent class. In C++, virtual functions are declared explicitly using the virtual keyword while in Java, all non-static methods are virtual by default unless they are explicitly marked as final.

Syntax

C++:

class ParentClass {
    public:
        virtual void foo() {
            // default implementation
        }
}

class ChildClass : public ParentClass {
    public:
        void foo() override {
            // custom implementation
        }
}

Java:

public class ParentClass {
    public void foo() {
        // default implementation
    }
}

public class ChildClass extends ParentClass {
    public void foo() {
        // custom implementation
    }
}

Example

C++:

#include <iostream>
using namespace std;

class ParentClass {
    public:
        virtual void foo() {
            cout << "Hello from ParentClass" << endl;
        }
};

class ChildClass : public ParentClass {
    public:
        void foo() override {
            cout << "Hello from ChildClass" << endl;
        }
};

int main() {
    ParentClass* obj1 = new ParentClass();
    obj1->foo();

    ChildClass* obj2 = new ChildClass();
    obj2->foo();

    ParentClass* obj3 = new ChildClass();
    obj3->foo();

    return 0;
}

Java:

public class ParentClass {
  public void foo() {
    System.out.println("Hello from ParentClass");
  }
}

public class ChildClass extends ParentClass {
  public void foo() {
    System.out.println("Hello from ChildClass");
  }
}

public class Main {
  public static void main(String[] args) {
    ParentClass obj1 = new ParentClass();
    obj1.foo();

    ParentClass obj2 = new ChildClass();
    obj2.foo();

    ChildClass obj3 = new ChildClass();
    obj3.foo();
  }
}

Output

C++:

Hello from ParentClass
Hello from ChildClass
Hello from ChildClass

Java:

Hello from ParentClass
Hello from ChildClass
Hello from ChildClass

Explanation

In both examples, we have a ParentClass with a default implementation of the foo() method and a ChildClass with its own implementation of the foo() method. We create objects of ParentClass and ChildClass and call the foo() method to see the output.

In both cases, when we create an object of ChildClass and call the foo() method, we get the custom implementation defined in the ChildClass. When we create an object of ParentClass and call the foo() method, we get the default implementation defined in the ParentClass. When we create an object of ChildClass and assign it to a reference of ParentClass and call the foo() method, we get the custom implementation defined in the ChildClass.

Use

Virtual functions are used to achieve polymorphism in object-oriented programming. They allow a subclass to provide its own implementation of a method defined in the parent class, which is useful when we want to work with objects of different subclasses using a common interface.

Important Points

  • Virtual functions in C++ are declared using the virtual keyword while in Java, all non-static methods are virtual by default unless they are marked as final.
  • Virtual functions allow a subclass to provide its own implementation of a method defined in the parent class, which is useful in achieving polymorphism.
  • Virtual functions can be accessed through a reference or pointer to an object of the parent class.

Summary

In summary, virtual functions in C++ and JAVA allow a subclass to provide its own implementation of a method defined in the parent class. This is useful in achieving polymorphism and working with objects of different subclasses using a common interface. The default virtual behavior differs slightly between C++ and JAVA, but achieves the same effect in both languages.

Published on: