What is subtyping in cpp | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is subtyping in cpp

16th Mar 2022, 6:54 AM
Sufia Hashmi
Sufia Hashmi - avatar
3 Answers
+ 2
Subtyping is a method for substitution and code reuse used in object-oriented programming languages to prevent unnecessary copying of largely similar code and promote code readability and prevent bugs. From Internet. Hope it helps! Happy Coding 🤠
16th Mar 2022, 7:26 AM
Adil
Adil - avatar
+ 2
Is it same as inheritance?
16th Mar 2022, 10:54 AM
Sufia Hashmi
Sufia Hashmi - avatar
+ 1
Both are similar, but not the same. Subtyping means writing a class B which conforms to A's interface, as well as possibly adding some new methods of its own. Per Liskov's substitution principle1, in any context where an A is expected we can supply a B instead. Inheritance means writing a class B that specialises A to a particular use, by reusing some of its behaviour and perhaps overriding parts. It is further explained here, you can check it out. https://counterexamples.org/subtyping-vs-inheritance.html
16th Mar 2022, 11:05 AM
Adil
Adil - avatar