{"id":191535,"date":"2021-03-01T16:00:05","date_gmt":"2021-03-01T13:00:05","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-covariance-and-contravariance-in-programming-cloudsavvy-it\/"},"modified":"2021-03-01T16:00:05","modified_gmt":"2021-03-01T13:00:05","slug":"what-is-covariance-and-contravariance-in-programming-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-covariance-and-contravariance-in-programming-cloudsavvy-it\/","title":{"rendered":"#What Is Covariance and Contravariance in Programming? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a384929d8e63\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a384929d8e63\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-covariance-and-contravariance-in-programming-cloudsavvy-it\/#Wiring_Interfaces_Together\" >Wiring Interfaces Together<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-covariance-and-contravariance-in-programming-cloudsavvy-it\/#Looking_at_Contravariance\" >Looking at Contravariance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-covariance-and-contravariance-in-programming-cloudsavvy-it\/#Handling_Variance_Issues\" >Handling Variance Issues<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-covariance-and-contravariance-in-programming-cloudsavvy-it\/#Other_Forms_of_Variance\" >Other Forms of Variance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-covariance-and-contravariance-in-programming-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Is Covariance and Contravariance in Programming? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9909\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/a8346366b9908564d29ccb5d57d7a606\/p\/uploads\/2021\/02\/cdfdf979.jpg\" alt=\"Abstract image showing expanding yellow rectangles\" width=\"1584\" height=\"796\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Covariance and contravariance are terms which describe how a programming language handles subtypes. The variance of a type determines whether its subtypes can be used interchangeably with it.<\/p>\n<p>Variance is a concept that can seem opaque until a concrete example is provided. Let\u2019s consider a base type <code>Animal<\/code> with a subtype of <code>Dog<\/code>.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">interface<\/span> Animal <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> walk<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"kw2\">interface<\/span> Dog <span class=\"kw2\">extends<\/span> Animal <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> bark<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>All \u201cAnimals\u201d can walk, but only \u201cDogs\u201d can bark. Now, let\u2019s consider what h<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ens when this object hierarchy is used in our application.<\/p>\n<h2 id=\"wiring-interfaces-together\"><span class=\"ez-toc-section\" id=\"Wiring_Interfaces_Together\"><\/span>Wiring Interfaces Together<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Since every <code>Animal<\/code> can walk, we can create a generic interface that exercises any <code>Animal<\/code>.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">interface<\/span> AnimalController <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> exercise<span class=\"br0\">(<\/span>Animal <span class=\"re0\">$Animal<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>AnimalController<\/code> has an <code>exercise()<\/code> method that typehints the <code>Animal<\/code> interface.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">interface<\/span> DogRepository <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getById<span class=\"br0\">(<\/span>int <span class=\"re0\">$id<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> Dog<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Now we have a <code>DogRepository<\/code> with a method that is guaranteed to return a <code>Dog<\/code>.<\/p>\n<p>What happens if we try to use this value with the <code>AnimalController<\/code>?<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$AnimalController<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">exercise<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$DogRepository<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">getById<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This is permissible in languages where covariant parameters are supported. <code>AnimalController<\/code> has to receive an <code>Animal.<\/code>\u00a0What we\u2019re passing is actually a <code>Dog,<\/code> but it still satisfies the <code>Animal<\/code> contract.<\/p>\n<p>This kind of relationship is particularly important when you\u2019re extending classes. We might want a generic <code>AnimalRepository<\/code> that retrieves any animal without its species details.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">interface<\/span> AnimalRepository <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getById<span class=\"br0\">(<\/span>int <span class=\"re0\">$id<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> Animal<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"kw2\">interface<\/span> DogRepository <span class=\"kw2\">extends<\/span> AnimalRepository <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getById<span class=\"br0\">(<\/span>int <span class=\"re0\">$id<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> Dog<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><code>DogRepository<\/code> modifies the contract of <code>AnimalRepository<\/code>\u2014as callers will get a <code>Dog<\/code> instead of an <code>Animal<\/code>\u2014but doesn\u2019t fundamentally change it. It\u2019s just being more specific about its return type. A <code>Dog<\/code> is still an <code>Animal.<\/code>\u00a0The types are covariant, so <code>DogRepository<\/code>\u2018s definition is acceptable.<\/p>\n<h2 id=\"looking-at-contravariance\"><span class=\"ez-toc-section\" id=\"Looking_at_Contravariance\"><\/span>Looking at Contravariance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let\u2019s now consider the inverse example. It might be desirable to have a <code>DogController,<\/code> which alters the way in which \u201cDogs\u201d are exercised. Logically, this could still extend the <code>AnimalController<\/code> interface. However, in practice, most languages won\u2019t allow you to override <code>exercise()<\/code> in the necessary way.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">interface<\/span> AnimalController <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> exercise<span class=\"br0\">(<\/span>Animal <span class=\"re0\">$Animal<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"kw2\">interface<\/span> DogController <span class=\"kw2\">extends<\/span> AnimalController <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> exercise<span class=\"br0\">(<\/span>Dog <span class=\"re0\">$Dog<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>In this example, <code>DogController<\/code> has specified that <code>exercise()<\/code> only accepts a <code>Dog<\/code>. This conflicts with the upstream definition in <code>AnimalController<\/code>, which permits any \u201cAnimal\u201d to be passed. To satisfy the contract, <code>DogController<\/code> must, therefore, also accept any <code>Animal<\/code>.<\/p>\n<p>At first glance, this can seem confusing and unhelpful. The reasoning behind this restriction becomes more clear when you\u2019re typehinting against <code>AnimalController<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">function<\/span> exerciseAnimal<span class=\"br0\">(<\/span>\n    AnimalController <span class=\"re0\">$AnimalController<\/span><span class=\"sy0\">,<\/span>\n    AnimalRepository <span class=\"re0\">$AnimalRepository<\/span><span class=\"sy0\">,<\/span>\n    int <span class=\"re0\">$id<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"re0\">$AnimalController<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">exercise<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$AnimalRepository<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">getById<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$id<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The problem is that <code>AnimalController<\/code> could be an <code>AnimalController<\/code> or a <code>DogController<\/code>\u2014our method isn\u2019t to know which interface implementation it\u2019s using. This is down to the same rules of covariance which were useful earlier.<\/p>\n<p>As <code>AnimalController<\/code> <em>might<\/em> be a <code>DogController<\/code>, there\u2019s now a serious runtime bug awaiting discovery. <code>AnimalRepository<\/code> always returns an <code>Animal<\/code>, so if <code>$AnimalController<\/code> <em>is<\/em> a <code>DogController<\/code>, the application is going to crash. The <code>Animal<\/code> type is too vague to pass to the <code>DogController<\/code> <code>exercise()<\/code> method.<\/p>\n<p>It\u2019s worth noting that languages that support method overloading would accept <code>DogController<\/code>. Overloading permits you to define multiple methods with the same name, provided that they have different <em>signatures<\/em> (They have different parameter and\/or return types.). <code>DogController<\/code> would have an extra <code>exercise()<\/code> method that only accepted \u201cDogs.\u201d However, it would also need to implement the upstream signature accepting any \u201cAnimal.\u201d<\/p>\n<h2 id=\"handling-variance-issues\"><span class=\"ez-toc-section\" id=\"Handling_Variance_Issues\"><\/span>Handling Variance Issues<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>All of the above can be summarised by saying that function return types are allowed to be <em>covariant,\u00a0<\/em>while argument types should be <em>contravariant<\/em>. This means that a function may return a more specific type than the interface defines. It may also accept a more abstract type as an argument (although most popular programming languages don\u2019t implement this).<\/p>\n<p>You most often encounter variance issues while working with generics and collections. In these scenarios, you often want an <code>AnimalCollection<\/code> and a <code>DogCollection<\/code>. Should <code>DogCollection<\/code> extend <code>AnimalCollection<\/code>?<\/p>\n<p>Here\u2019s what these interfaces could look like:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">interface<\/span> AnimalCollection <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> add<span class=\"br0\">(<\/span>Animal <span class=\"re0\">$a<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getById<span class=\"br0\">(<\/span>int <span class=\"re0\">$id<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> Animal<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"kw2\">interface<\/span> DogCollection <span class=\"kw2\">extends<\/span> AnimalCollection <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> add<span class=\"br0\">(<\/span>Dog <span class=\"re0\">$d<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getById<span class=\"br0\">(<\/span>int <span class=\"re0\">$id<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> Dog<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Looking first at <code>getById()<\/code>, <code>Dog<\/code> is a subtype of <code>Animal<\/code>. The types are covariant, and covariant return types are allowed. This is acceptable. We observe the variance issue again with <code>add()<\/code> though\u2014<code>DogCollection<\/code> must allow any <code>Animal<\/code> to be added in order to satisfy the <code>AnimalCollection<\/code> contract.<\/p>\n<p>This issue is usually best addressed by making the collections immutable. Only allow new items to be added in the collection\u2019s constructor. You can then eliminate the <code>add()<\/code> method altogether, making <code>AnimalCollection<\/code> a valid candidate for <code>DogCollection<\/code> to inherit from.<\/p>\n<h2 id=\"other-forms-of-variance\"><span class=\"ez-toc-section\" id=\"Other_Forms_of_Variance\"><\/span>Other Forms of Variance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Besides covariance and contravariance, you may also come across the following terms:<\/p>\n<ul>\n<li><strong>Bivariant:\u00a0<\/strong>A type system is bivariant if both covariance and contravariance simultaneously apply to a type relationship. Bivariance was used by TypeScript for its parameters prior to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/release-notes\/typescript-2-6.html\">TypeScript 2.6<\/a><\/li>\n<li><strong>Variant:\u00a0<\/strong>Types are variant if either covariance or contravariance applies.<\/li>\n<li><strong>Invariant:<\/strong>\u00a0Any types that are not variant.<\/li>\n<\/ul>\n<p>You\u2019ll usually be working with covariant or contravariant types. In terms of class inheritance, a type B is covariant with a type A if it <em>extends<\/em> A. A type B is contravariant with a type A if it\u2019s the ancestor to B.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Variance is a concept that explains the limitations within type systems. Usually, you only need to remember that covariance is accepted in return types, whereas contravariance is used for parameters.<\/p>\n<p>The rules of variance arise from the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Liskov_substitution_principle\">Liskov substitution principle<\/a>. This states that you should be able to replace instances of a class with instances of its subclasses without altering any of the properties of the wider system. That means that if Type B extends Type A, instances of <code>A<\/code> may be substituted with instances of <code>B<\/code>.<\/p>\n<p>Using our example above means that we must be able to substitute <code>Animal<\/code> with <code>Dog<\/code>, or <code>AnimalController<\/code> with <code>DogController<\/code>. Here, we see again why <code>DogController<\/code> cannot override <code>exercise()<\/code> to only accept Dogs\u2014we\u2019d no longer be able to substitute <code>AnimalController<\/code> with <code>DogController<\/code>, as consumers currently passing an <code>Animal<\/code> would now need to provide a <code>Dog<\/code> instead. Covariance and contravariance enforce the LSP and ensure consistent standards of behavior.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/9904\/what-is-covariance-and-contravariance-in-programming\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Is Covariance and Contravariance in Programming? \u2013 CloudSavvy IT&#8221; Covariance and contravariance are terms which describe how a programming language handles subtypes. The variance of a type determines whether its subtypes can be used interchangeably with it. Variance is a concept that can seem opaque until a concrete example is provided. Let\u2019s consider a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":191536,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/a8346366b9908564d29ccb5d57d7a606\/p\/uploads\/2021\/02\/cdfdf979.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-191535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/191535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=191535"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/191535\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/191536"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=191535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=191535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=191535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}