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
Release: 2023-09-03 12:57:07
forward
1155 people have browsed it

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);
   }
}
Copy after login

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");
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template