{"id":155,"date":"2014-04-07T05:23:16","date_gmt":"2014-04-07T13:23:16","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=155"},"modified":"2019-05-14T11:19:08","modified_gmt":"2019-05-14T19:19:08","slug":"interfaces-and-abstract-class","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=155","title":{"rendered":"Interfaces and Abstract Class"},"content":{"rendered":"<p>Use abstraction if you have default methods (with accompanying code) for inheritors. Use interfaces if you just need to make sure that classes inheriting from this parent should implement all methods defined.<\/p>\n<p>I use abstract classes when I want the inheriting classes to inherit some functionality, and interfaces when I want to set some minimum structural criteria for a group of classes.<\/p>\n<p>One thing to remember is that any given class can &#8220;inherit&#8221; (technically implement) many interfaces but only one sub-class (be that abstract or not).<\/p>\n<p>Interface is a contract whereas Abstract Class is actually a class. Objects cannot be instantiated in either Interface or Abstract Classes.<\/p>\n<p>PHP interfaces can have constants, but not properties (instance variables). If you don&#8217;t need to modify your &#8220;property&#8221;, you can use a constant instead.<\/p>\n<p>For interfaces also talk about &#8220;extending&#8221; interfaces and &#8220;multiple interface inheritance&#8221;<\/p>\n<p>Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method&#8217;s signature &#8211; they cannot define the implementation.<\/p>\n<p>&#8211; Abstract classes can have consts, members, method stubs and defined methods, whereas interfaces can only have consts and methods stubs.<br \/>\n&#8211; Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public.<br \/>\n&#8211; When inheriting an abstract class, the child class must define the abstract methods, whereas an interface can extend another interface and methods don\u2019t have to be defined.<br \/>\n&#8211; A child class can only extend a single abstract (or any other) class, whereas an interface can extend or a class can implement multiple other interfaces. Abstract class can extend another abstract class though.<br \/>\n&#8211; A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility.<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"zRtv4hE8HE\"><p><a href=\"https:\/\/webrewrite.com\/abstract-class-vs-interfaces-php\/\">Abstract Class vs Interface in PHP<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Abstract Class vs Interface in PHP&#8221; &#8212; Web Rewrite\" src=\"https:\/\/webrewrite.com\/abstract-class-vs-interfaces-php\/embed\/#?secret=zRtv4hE8HE\" data-secret=\"zRtv4hE8HE\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>Abstract Class<\/p>\n<pre>abstract class Animal { \r\n  function greeting() { \r\n    $sound = $this-&gt;sound();      \/\/ exists in child class by contract \r\n    return strtoupper($sound); \r\n  } \r\n  abstract function sound();      \/\/ this is the contract \r\n} \r\nclass Dog extends Animal { \r\n  function sound() {              \/\/ concrete implementation is mandatory \r\n    return \"Woof!\"; \r\n  } \r\n}\r\n<\/pre>\n<p>Interface<\/p>\n<pre>interface animal {\r\nfunction breath();\r\nfunction eat();\r\n}\r\nNote: the interface\u2019s functions\/methods cannot have the details\/guts filled in \u2013 that is left to the class that uses the interface.\r\nExample of a class using an interface:\r\nclass dog implements animal{\r\nfunction bark() {\r\necho \u201cyap, yap, yap \u2026\u201d;\r\n}\r\n\/* the interface methods\/functions must be implemented (given their \u2018guts\u2019) in the class *\/\r\nfunction breath() { echo \u201cdog is breathing \u2026\u201d;}\r\nfunction eat() { echo \u201cdog is easting \u2026\u201d;}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Use abstraction if you have default methods (with accompanying code) for inheritors. Use interfaces if you just need to make sure that classes inheriting from this parent should implement all methods defined. I use abstract classes when I want the inheriting classes to inherit some functionality, and interfaces when I want to set some minimum &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=155\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Interfaces and Abstract Class<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155"}],"version-history":[{"count":13,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":1012,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions\/1012"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}