多态这个词意味着有多种形式。简单来说,我们可以将多态性定义为消息以多种形式显示的能力。在本文中,我们将看到两种类型的多态性,编译时和运行时之间的区别。
编译时多态性:每当对象在编译时与其功能绑定时,这称为编译时多态性。在编译时, Java通过检查方法签名知道要调用哪个方法。所以这被称为编译时多态性或静态或早期绑定。编译时多态是通过方法重载实现的。方法重载表示在一个具有不同原型的类中可以有多个同名的函数。函数重载是实现多态的方法之一,但它取决于我们采用哪种类型的多态的技术。在Java,我们在编译时实现函数重载。下面是一个可以观察到编译时多态性的示例。
Java
// Java program to demonstrate
// compile-time polymorphism
public class GFG {
// First addition function
public static int add(int a, int b)
{
return a + b;
}
// Second addition function
public static double add(
double a, double b)
{
return a + b;
}
// Driver code
public static void main(String args[])
{
// Here, the first addition
// function is called
System.out.println(add(2, 3));
// Here, the second addition
// function is called
System.out.println(add(2.0, 3.0));
}
}
Java
// Java program to demonstrate
// runtime polymorphism
// Implementing a class
class Test {
// Implementing a method
public void method()
{
System.out.println("Method 1");
}
}
// Defining a child class
public class GFG extends Test {
// Overriding the parent method
public void method()
{
System.out.println("Method 2");
}
// Driver code
public static void main(String args[])
{
Test test = new GFG();
test.method();
}
}
输出:
5
5.0
运行时多态性:当对象在运行时与功能绑定时,这称为运行时多态性。运行时多态可以通过方法覆盖来实现。 Java虚拟机确定在运行时调用的正确方法,而不是在编译时。它也称为动态或后期绑定。方法覆盖表示子类具有与父类中声明的方法相同的方法。这意味着如果子类提供了由其父类之一提供的方法的特定实现,那么它被称为方法覆盖。以下是可以观察到运行时多态性的示例。
Java
// Java program to demonstrate
// runtime polymorphism
// Implementing a class
class Test {
// Implementing a method
public void method()
{
System.out.println("Method 1");
}
}
// Defining a child class
public class GFG extends Test {
// Overriding the parent method
public void method()
{
System.out.println("Method 2");
}
// Driver code
public static void main(String args[])
{
Test test = new GFG();
test.method();
}
}
输出:
Method 2
下表展示了运行时多态性和编译时多态性之间的区别:
Sr.No | Compile Time Polymorphism | Run time Polymorphism |
---|---|---|
1 | In Compile time Polymorphism, the call is resolved by the compiler. | In Run time Polymorphism, the call is not resolved by the compiler. |
2 | It is also known as Static binding, Early binding and overloading as well. | It is also known as Dynamic binding, Late binding and overriding as well. |
3 | Method overloading is the compile-time polymorphism where more than one methods share the same name with different parameters or signature and different return type. | Method overriding is the runtime polymorphism having same method with same parameters or signature, but associated in different classes. |
4 | It is achieved by function overloading and operator overloading. | It is achieved by virtual functions and pointers. |
5 | It provides fast execution because the method that needs to be executed is known early at the compile time. | It provides slow execution as compare to early binding because the method that needs to be executed is known at the runtime. |
6 | Compile time polymorphism is less flexible as all things execute at compile time. | Run time polymorphism is more flexible as all things execute at run time. |