Default Virtual Behaviour in C++ vs Java
Virtual Behaviour in C++:
- The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual.
- The virtual class can be called and executed in the derived class using a pointer or reference once the function has been declared in the base class. As a result, it requests that the compiler generate a function binding and identify the type of the object at runtime (late binding or dynamic linkage).
- In C++, a virtual function ensures that the proper function is called when using a reference or pointer. Only one pointer can be used in C++ programming to refer to all objects of derived classes. The function in the base class will always be called since the pointer contains references to all the objects from which it was derived.
- A virtual function in C++ can assist us in getting around this with the virtual version of the derived class.
To build a virtual function in C++, we must adhere to a few restrictions. They are as follows:
- There can be no static functions.
- Using the term "virtual," we can deduce them.
- In C++, virtual functions must be base class members. They may also be buddy functions of other classes.
- Both the base class and any derived classes should have the same prototype for these functionalities.
- Object pointers allow access to virtual functions.
Program:
#include <iostream>
using namespace std;
class Demo{
virtual void Display()
{
cout << "Hello" << endl;
}
void Show(){
cout << "Welcome" << endl;
}};
class Derived: public Demo{
void Display()
{
cout << "Hi" << endl;
}
void Show()
{
cout << "Thank You" << endl;
}};
int main(){
Demo* bptr;
Derived dptr;
bptr = &dptr;
bpointr->Display();
bpointr->Show();
return 0;
}
Output:
Hi
Welcome
Virtual Behaviour in Java:
Virtual Methods are Java methods that aren't static and don't have the word Final in front of them. In Java, all methods are, by default, virtual. Because child classes in Java can override the methods of their parent classes if the overridden function is non-static and has the same method signature, virtual methods are crucial to polymorphism.
There are, however, certain non-virtual ways. For instance, a method is not virtual if it is marked private or uses the term final.
- As a result, any non-static Java method other than final and private methods is by definition a virtual method. Virtual methods are not those that cannot be inherited due to polymorphic behaviour.
Observations on Java's Virtual function:
- The Virtual Function is a typical Java function.
- To define the virtual function, we are not obliged to specify any specific description.
- Java does not utilise the virtual keyword to define the virtual function.
- The object of the child class is referred to via the Parent class pointer.
- The virtual function has to have the same name as its parent class definition in the child class.
- With the exception of the final keyword and some access modifiers, all instance methods in Java are thought of as virtual functions.
Program:
import java.util.*;
class Demo
{
void sleep()
{
System.out.println("I sleep at night ");
}
public static void main(String[] args)
{
List<Demo> a = new LinkedList<Demo >();
a.add(new Demo());
a.add(new Ravi());
a.add(new Sai());
a.add(new Jack())
for (Demo c: a)
{
c.sleep();
}
}
}
public class Ravi extends Demo
{
public void sleep()
{
System.out.println("Ravi sleeps at night”);
}
}
public class Sai extends Demo
{
public void sleep()
{
System.out.println("Sai sleeps at night”);
}
}
public class Jack extends Sai
{
public void sleep()
{
System.out.println("Jack sleeps at night”);
}
}
Output:
I sleep at night
Ravi sleeps at night
Sai sleeps at night
Jack sleeps at night
Pure Virtual Function in Java:
Pure virtual functions refer to a virtual function for which we are not needed to implement it. For instance, Java's Abstract method is a wholly virtual function.
Program:
Import java.io.*;
abstract class Dog
{
final void sleep()
{
System.out.println("Dog");
}
abstract void eat();
}
class Puppy extends Dog
{
void eat()
{
System. out.println("Puppy is eating");
}
}
class Demo {
public static void main(String args[]){
Dog a = new Puppy();
a.sleep();
}
}
Output:
Puppy is eating