Default Virtual Behaviour in C++ and JAVAClass 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." C++Output Compilation Error 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. JAVAOutput 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.Next TopicC++ hashing programme with chaining |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India