📜  C++ 和Java中的默认虚拟行为有何不同?

📅  最后修改于: 2022-05-13 01:55:23.936000             🧑  作者: Mango

C++ 和Java中的默认虚拟行为有何不同?

让我们讨论一下 C++ 和Java中方法的默认虚拟行为是如何相反的。记住这一点非常重要 默认情况下,C++ 语言类成员方法是非虚拟的。可以使用虚拟关键字将它们变为虚拟。例如, Base::show()在下面的程序中是非虚拟的,程序打印“Base::show() called”

例子:

CPP
// C++ Program to Illustrate How
// Default Virtual Behave
// Different in C++ and Java
  
// Importing required libraries
// Input output stream
#include 
  
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;
}


Java
// 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();
    }
}


输出:编译错误

Base::show() called

输出说明:在 Base::show() 定义之前添加 virtual 使程序打印“Derived::show() called”。在Java中,方法默认是虚拟的,可以使用 final 关键字将其设为非虚拟。例如,在下面的Java程序中,show() 默认是虚拟的,程序打印“Derived::show() called ”。

通过下面提出的示例,让我们看看Java编程语言使用相同概念的情况下会发生什么。

例子:

Java

// 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();
    }
}
输出
Derived::show() called