{"id":134146,"date":"2020-12-15T17:00:42","date_gmt":"2020-12-15T14:00:42","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-does-an-interface-do-in-object-oriented-programming-cloudsavvy-it\/"},"modified":"2020-12-15T17:00:42","modified_gmt":"2020-12-15T14:00:42","slug":"what-does-an-interface-do-in-object-oriented-programming-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-does-an-interface-do-in-object-oriented-programming-cloudsavvy-it\/","title":{"rendered":"#What Does An Interface Do in Object-Oriented 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-6a42ca13adff6\" 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-6a42ca13adff6\" 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-does-an-interface-do-in-object-oriented-programming-cloudsavvy-it\/#Formalized_Polymorphism\" >Formalized Polymorphism<\/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-does-an-interface-do-in-object-oriented-programming-cloudsavvy-it\/#Using_Interfaces\" >Using Interfaces<\/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-does-an-interface-do-in-object-oriented-programming-cloudsavvy-it\/#Writing_Your_Own_Interfaces\" >Writing Your Own Interfaces<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Does An Interface Do in Object-Oriented Programming? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6018\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/d9bee69b242da6fb57907d0799a9fdf4\/p\/uploads\/2020\/08\/b476fc9b.png\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>One of the key components of Object-oriented languages like Java and C# is the ability to write classes using interfaces, which standardize method definitions and enable enhanced polymorphism. We\u2019ll discuss what they are, and how to use them.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Formalized_Polymorphism\"><\/span>Formalized Polymorphism<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Interfaces are basically classes without code. The can define properties and methods (though not fields directly) without actually storing data or writing an implementation for functions.<\/p>\n<p>For example, .NET\u2019s IEnumerable interface is very common. All the it requires is a function that returns an IEnumerator, which can be used to iterate over a collection.<\/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=\"csharp\">\n<ol>\n<li class=\"li1\">\n<pre class=\"de1\"><span class=\"kw1\">public<\/span> <span class=\"kw4\">interface<\/span> IEnumerable<\/pre>\n<\/li>\n<li class=\"li1\">\n<pre class=\"de1\"><span class=\"br0\">{<\/span><\/pre>\n<\/li>\n<li class=\"li1\">\n<pre class=\"de1\">    IEnumerator GetEnumerator<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/li>\n<li class=\"li1\">\n<pre class=\"de1\"><span class=\"br0\">}<\/span><\/pre>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Every collection in .NET\u00a0<em>implements<\/em> this interface. In fact, most collections will implement a lot of interfaces, all standardizing the methods used to work with them.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8575\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/43f7aa32a331ff6880288c43b085393b\/p\/uploads\/2020\/12\/4cb31335.png\" alt=\"\" width=\"700\" height=\"198\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The benefit of doing it this way is that all the methods are standardized in the interface, so that any time you want something to accept many kinds of classes, you can do so by accessing the interface methods.<\/p>\n<p>For example, <code>foreach<\/code>\u00a0loops under the hood really just use <code>.GetEnumerator()<\/code>, so they will support any type of collection that implements <code>IEnumerable<\/code>, like Lists, Dictionaries, and HashSets.<\/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=\"csharp\">\n<ol>\n<li class=\"li1\">\n<pre class=\"de1\"><span class=\"kw4\">void<\/span> Main<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><\/pre>\n<\/li>\n<li class=\"li1\">\n<pre class=\"de1\"><span class=\"br0\">{<\/span><\/pre>\n<\/li>\n<li class=\"li1\">\n<pre class=\"de1\">    <span class=\"kw1\">foreach<\/span><span class=\"br0\">(<\/span><span class=\"kw1\">var<\/span> item <span class=\"kw1\">in<\/span>  IEnumerable<span class=\"br0\">)<\/span> <\/pre>\n<\/li>\n<li class=\"li1\">\n<pre class=\"de1\">    <span class=\"br0\">{<\/span><\/pre>\n<\/li>\n<li class=\"li1\">\n<pre class=\"de1\">       <span class=\"co1\">\/\/ do something<\/span><\/pre>\n<\/li>\n<li class=\"li1\">\n<pre class=\"de1\">    <span class=\"br0\">}<\/span><\/pre>\n<\/li>\n<li class=\"li1\">\n<pre class=\"de1\"><span class=\"br0\">}<\/span><\/pre>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>You can think of Interfaces as being a replacement for base classes. Base classes are still very useful, but requiring every class to inherit and override methods can be clunky. Plus, you can only have one base class, but you can implement any number of interfaces.<\/p>\n<p>Interfaces can also be used in place of type parameters. For example, you may have a custom collection, <code>CustomList&lt;T&gt;<\/code>. This works for any type, but if you wanted to call a specific method on each element, you can\u2019t, since the compiler has no idea if the type being used supports that method. The only options you\u2019ll get will be methods for basic <code>objects<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8585\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/707cd545ecc067a276760eabac863bb8\/p\/uploads\/2020\/12\/0cd91f67.png\" alt=\"\" width=\"669\" height=\"291\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>However, if you were to instead ditch the generic type parameter and use an interface, you can call a method on the items. The collection will still support any kind of type, though now each item you intend to put in it will need to implement the interface.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8588\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/6c9426c813e1a2a90bf9b6798389f754\/p\/uploads\/2020\/12\/b293be48.png\" alt=\"\" width=\"453\" height=\"247\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>, you should use interfaces when you\u2019re reusing the same methods for many classes that don\u2019t inherit from each other, and you\u2019d like to be able to write polymorphic code that doesn\u2019t care about the specific underlying class.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_Interfaces\"><\/span>Using Interfaces<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Interfaces are pretty simple to use. They use the same inheritance syntax as base classes, a colon after the class definition. Note that if you want to use an interface with a base class, the base class must come first, followed by a comma and then any interfaces it implements.<\/p>\n<p>Once you add the interface definition, you\u2019ll probably get an error. Visual Studio will tell you that you\u2019re not actually implementing the interface properties and methods. This can actually be very useful, as if you make any changes to the interface, you\u2019ll need to go and update all classes that implement it.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8586\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/a945e175d4097369cfabdc62f2a67ac2\/p\/uploads\/2020\/12\/76ea744f.png\" alt=\"\" width=\"700\" height=\"237\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/h2>\n<p>Visual Studio is pretty fancy, and if you click \u201cshow potential fixes,\u201d you\u2019ll get an option for VS to automatically add the interface boilerplate to your class. You\u2019ll obviously need to replace the NotImplementedExceptions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8589\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/78c7e390bf3896b8b6ccca185b6c7656\/p\/uploads\/2020\/12\/8cc922d3.png\" alt=\"\" width=\"628\" height=\"395\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can also implement interfaces\u00a0<em>explicitly.<\/em> This has the same effect, but with different syntax that makes it clear which interface the method or property is coming from. This looks pretty weird at first glance, but can be useful if you\u2019re implementing a lot of interfaces.<\/p>\n<p><em><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8590\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/5533e38dc1d61b105ad9c64e6ec32674\/p\/uploads\/2020\/12\/389e4525.png\" alt=\"\" width=\"628\" height=\"393\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/em><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Writing_Your_Own_Interfaces\"><\/span>Writing Your Own Interfaces<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Interface definitions are pretty much the same as class definitions, though each method will not have a body, and you\u2019ll need to use \u201cinterface,\u201d instead of \u201cclass,\u201d obviously. Much like classes, interfaces can also use generic type parameters, which can be useful for custom collections.<\/p>\n<p><em><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8591\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/7f5a2d394ff62ec7e670c8c3ea5928a0\/p\/uploads\/2020\/12\/ee0fed07.png\" alt=\"\" width=\"380\" height=\"168\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/em><\/p>\n<p>You can use the <code>{ get; set; }<\/code>\u00a0syntax to specify properties implicitly, which also works when implementing them in the actual class:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8592\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/76c5040c5ef34b450b1babe33771e99a\/p\/uploads\/2020\/12\/772e58fc.png\" alt=\"\" width=\"412\" height=\"229\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can also have interfaces inherit each other. For example, this <code>ICustomList<\/code>\u00a0interface could be a lot more useful if it simply including all other definitions that <code>List<\/code>\u00a0uses, but adds a few custom ones. Now, when you go to use the interface, you\u2019ll need to implement all the interface members from every interface it inherits from.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8594\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/5de63eadddfe0d6e0cd44cce5323f0f4\/p\/uploads\/2020\/12\/1ce2e499.png\" alt=\"\" width=\"700\" height=\"303\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/>\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\/8569\/what-does-an-interface-do-in-object-oriented-programming\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Does An Interface Do in Object-Oriented Programming? \u2013 CloudSavvy IT&#8221; One of the key components of Object-oriented languages like Java and C# is the ability to write classes using interfaces, which standardize method definitions and enable enhanced polymorphism. We\u2019ll discuss what they are, and how to use them. Formalized Polymorphism Interfaces are basically classes&#8230;<\/p>\n","protected":false},"author":1,"featured_media":134147,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/08\/b476fc9b.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-134146","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\/134146","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=134146"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/134146\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/134147"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=134146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=134146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=134146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}