{"id":171,"date":"2018-01-30T06:51:00","date_gmt":"2018-01-30T05:51:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/01\/30\/what-are-interfaces-in-c-and-java-a-simple-explanation\/"},"modified":"2020-08-22T09:17:30","modified_gmt":"2020-08-22T07:17:30","slug":"what-are-interfaces-in-c-and-java-a-simple-explanation","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/what-are-interfaces-in-c-and-java-a-simple-explanation\/","title":{"rendered":"What are Interfaces in C# and Java? A Simple Explanation"},"content":{"rendered":"<div style=\"color: #555555; font-size: 18px; line-height: 30px; text-align: justify;\">\n<div style=\"font-family: 'segoe ui';\">In this lesson we would cover the following:<\/p>\n<ol>\n<li><b>What are Interfaces?<\/b><\/li>\n<li><b>When can an Interface be used?<\/b><\/li>\n<li><b>Interface implementation example in C#<\/b><\/li>\n<li><b>Summary on Interfaces <\/b><\/li>\n<li><b>Interview Questions<\/b><\/li>\n<ul><\/ul>\n<\/ol>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/2.bp.blogspot.com\/-DjEJ9ST15mU\/WnAVQ19SQHI\/AAAAAAAAA60\/0IbLZ7DMbeouuDW10_yxbtJC6pWgiEHAgCLcBGAs\/s1600\/Interface%2B%2Bin%2BC%2523%2Band%2BJava.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"741\" data-original-width=\"1513\" height=\"156\" src=\"https:\/\/2.bp.blogspot.com\/-DjEJ9ST15mU\/WnAVQ19SQHI\/AAAAAAAAA60\/0IbLZ7DMbeouuDW10_yxbtJC6pWgiEHAgCLcBGAs\/s320\/Interface%2B%2Bin%2BC%2523%2Band%2BJava.jpg\" width=\"320\" \/><\/a><\/div>\n<p><b>What are Interfaces?<\/b><br \/>An interface is a class that is marked with the Interface keyword. According to the C# programming guide, an interface contains definitions for a group of related functionalities.. Maybe I could correct this by saying that, &#8216;an interface contains <span style=\"color: #cc0000;\"><i>only <\/i><\/span>definitions&#8230;&#8217;. This means that interfaces would not contain any implementation.<\/p>\n<p><b>When do we Need an Interface?<\/b><br \/>Interface is use when you want a class to inherit from more than one base classes. this capability, where a sub class inherits from more than on parent is called Multiple Inheritance. This is&nbsp; a feature in C++ but not in C#. Interface is a way to achieve multiple inheritance in C#.<br \/>Interface is defined using the interface keywork<\/p>\n<p><b>Example of Interface in C#<\/b><br \/>In the example below, there are two interfaces called IMovable and IParkable. The class Car is defined to&nbsp; inherit from the two Interfaces.<br \/>Notice the the Car Class implements the methods in the two interfaces <\/p>\n<pre>   <br \/><br \/>    public interface IMovable<br \/>    {<br \/>         bool Equals();<br \/>    }<br \/><br \/>    public interface IParkable<br \/>    {<br \/>        String Park();<br \/>    }<br \/><br \/>    public class Car : IMovable, IParkable<br \/>    {<br \/>        \/\/ Implementation of Imovable<br \/>        public bool Equals()<br \/>        {           <br \/>                return false;<br \/>        }<br \/><br \/>        public String Park()<br \/>        {<br \/>            return \"I need a garage\";<br \/>        }<br \/>    }<br \/><\/pre>\n<p> <b>Summary on Interfaces<\/b><br \/>1. Any class that implements an interface must also implement <i>all <\/i>the members of that interface.<br \/>2. Interfaces can contain both methods and properties <br \/>2. Interfaces cannot be instantiated directly.<br \/>A class can implement more than one interface.<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson we would cover the following: What are Interfaces? When can an Interface be used? Interface implementation example in C# Summary on Interfaces &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[363],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/171"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":1439,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions\/1439"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}