简体   繁体   中英

compile time polymorphism?

The book im reading says when you override a method in a subclass it means runtime polymorphism. Is there a thing like compile time polymorphism? I thought simple inheritance happens at compile time then polymorphism is runtime.

They are hinting at method overloading , which is sometimes referred to as a compile-time polymorphism .

Indeed, method overloading lets you invoke different pieces of code based on the types of objects passed in as parameters. Overloads are resolved at compile time, so the mechanism of overloading could be thought of as a compile-time polymorphism.

Lucca Cardelli has a great article named On Understanding Types, Data Abstraction and Polymorphism that explains the answer to your question in great detail.

What you mention in your question is called ad-hoc polymorphism and comprises of method overloading and type cohercions (like those in operator overloading), these two happen at compile time. On the other hand, choosing the right implementation for a given method happens at runtime, this is part of what is called parametric or inclusion polymorphism .

But I am pretty sure Lucca Cardelli will give you a better answer than what I could ever dream of to give.

Recently I participated in another similar question in this forum and I provided some examples of these two types of polymorphism. You might like to give it a look, although I have the impression this is something you may already know.

You may also like to read Polymorphism in Object-oriented Programming languages .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM