Home >Java >javaTutorial >What is the difference between compile-time polymorphism and run-time polymorphism?

What is the difference between compile-time polymorphism and run-time polymorphism?

WBOY
WBOYforward
2023-09-03 12:57:071121browse

What is the difference between compile-time polymorphism and run-time polymorphism?

Polymorphism is one of the most important OOP concepts. This is a concept where we can perform a single task in multiple ways. There are two types of polymorphism, one is compile-time polymorphism and the other is run-time polymorphism.

Method overloading is an example of compile-time polymorphism, and method overriding is an example of run-time polymorphism.

Method overloading is an example of compile-time polymorphism, and method overriding is an example of run-time polymorphism. p>

can be achieved through dynamic binding Determined to achieve

4.Inheritance is not involved InheritanceInvolving inheritance5ExampleMethod overriding is an example of compile-time polymorphismMethod overriding is an example of run-time polymorphism
Advanced. Key Compile-time polymorphism Run-time polymorphism
1

Basic

Compile-time polymorphism means binding occurs at compile time

Run-time polymorphism property, at runtime we know which method will be called

2

static/dynamicBinding

##can be achieved through static binding

Compile-time polymorphism example

public class Main {
   public static void main(String args[]) {
      CompileTimePloymorphismExample obj = new CompileTimePloymorphismExample();
      obj.display();
      obj.display("Polymorphism");
   }
}
class CompileTimePloymorphismExample {
   void display() {
      System.out.println("In Display without parameter");
   }
   void display(String value) {
      System.out.println("In Display with parameter" + value);
   }
}

Run-time polymorphism example

public class Main {
   public static void main(String args[]) {
      RunTimePolymorphismParentClassExample obj = new RunTimePolymorphismSubClassExample();
      obj.display();
   }
}

class RunTimePolymorphismParentClassExample {
   public void display() {
      System.out.println("Overridden Method");
   }
}

public class RunTimePolymorphismSubClassExample extends RunTimePolymorphismParentExample {

   public void display() {
      System.out.println("Overriding Method");
   }
}

The above is the detailed content of What is the difference between compile-time polymorphism and run-time polymorphism?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete