Default Virtual Behaviour In C++ And Java

Class member methods are non-virtual by default in C++. This means that by specifying it, they can be made virtual.

Methods in Java, on the other hand, are virtual by default and can be made non-virtual by using the 'final' keyword.

Let's look at how the default virtual behaviour of methods differs in C++ and Java. It is critical to remember that in the C++ programming language, class member methods are non-virtual by default. By using virtual keywords, they can be made virtual. In the following programme, for example, Base :: show is not virtual, and the programme prints "Base::show called."

Example

// C++ Program to Illustrate How
// Default Virtual Behave
// Different in C++ and Java

// Importing required libraries
// Input output stream
#include <iostream>

using namespace std;

// Class 1
// Superclass
class Base {

	// Granting public access via
	// public access modifier
public:
	// In c++, non-virtual by default
	// Method of superclass
	void show()
	{

		// Print statement
		cout << "Base::show() called";
	}
};

// Class 2
// Subclass
class Derived : public Base {

	// Granting public access via public access modifier
public:
	// Method of subclass
	void show()
	{

		// Print statement
		cout <<  "Derived :: show() called" ;
	}
};

// Main driver method
int main()
{
	// Creating object of subclass
	Derived d;
	
	// Creating object of subclass
	// with different reference
	Base& b = d;

	// Calling show() method over
	// Superclass object
	b.show();

	getchar();

	return 0;
}

Output

Compilation Error

Example

Base :: show() called

Explanation of Output: Adding virtual before the definition of Base::show causes the programme to print "Derived::show called." Methods in Java are virtual by default, but they can be made non-virtual by using the final keyword. In the following Java programme, for example, show is by default virtual, and the programme prints "Derived::show called."

Let's see what happens if we use the same concept in a java programming language, as shown in the example below.

Example

// Java Program to Illustrate
// How Default Virtual Behave
// Different in C++ and Java

// Importing required classes
import java.util.*;

// Class 1
// Helper class
class Base {

	// Method of sub class
	// In java, virtual by default
	public void show()
	{

		// Print statement
		System.out.println( "Base :: show() called" );
	}
}

// Class 2
// Helper class extending Class 1
class Derived extends Base {

	// Method
	public void show()
	{

		// Print statement
		System.out.println( "Derived :: show() called" );
	}
}

// Class 3
// Main class
public class GFG {

	// Main driver method
	public static void main(String[] args)
	{

		// Creating object of superclass with
		// reference to subclass object
		Base b = new Derived();
		;

		// Calling show() method over Superclass object
		b.show();
	}
}

Output

Output

Derived :: show() called

Note that, unlike non-virtual C++ behaviour, if we add final before the definition of show in Base, the above programme fails to compile.

Input Required

This code uses input(). Please provide values below: