{"id":1412,"date":"2021-11-17T22:38:59","date_gmt":"2021-11-17T21:38:59","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1412"},"modified":"2021-11-17T22:42:04","modified_gmt":"2021-11-17T21:42:04","slug":"extend-vs-implements-in-typescript","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/11\/17\/extend-vs-implements-in-typescript\/","title":{"rendered":"&#8216;extend&#8217; VS &#8216;implements&#8217; in TypeScript"},"content":{"rendered":"\n<p>What is the difference between&nbsp;<code>extends<\/code>&nbsp;and&nbsp;<code>implements<\/code> ?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Media {\n  format: string;\n}\nclass Video extends Media {}\nclass Image implements Media {}<\/code><\/pre>\n\n\n\n<p>To put in simpler terms:<\/p>\n\n\n\n<p><em><strong>extends: <\/strong><\/em>The class get all these methods and properties from the parent, so you don&#8217;t have to implement.<\/p>\n\n\n\n<p><em><strong>implements<\/strong><\/em>: The class has to implement methods and properties.<\/p>\n\n\n\n<p>other said :<\/p>\n\n\n\n<ul><li><strong><code>extends<\/code><\/strong>&nbsp;means:<\/li><\/ul>\n\n\n\n<p>The\u00a0<strong>new class is a child<\/strong>. It gets benefits coming with inheritance. It has all properties, methods as its parent. It can override some of these and implement new, but the parent stuff is already included. The child class (which is extended) will\u00a0<strong>inherit<\/strong>\u00a0all the properties and methods of the class it extends<\/p>\n\n\n\n<ul><li><strong><code>implements<\/code><\/strong>&nbsp;means:<\/li><\/ul>\n\n\n\n<p>The&nbsp;<strong>new class<\/strong>&nbsp;can be treated as&nbsp;<strong>the same&nbsp;<em>&#8220;shape&#8221;<\/em><\/strong>, while&nbsp;<strong>it is not a child<\/strong> : The class which uses the&nbsp;<code>implements<\/code>&nbsp;keyword will need to&nbsp;<strong>implement<\/strong>&nbsp;all the properties and methods of the class which it&nbsp;<code>implements<\/code><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Example<\/h4>\n\n\n\n<p>Let\u00b4s observe this short line of code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n    name: string;\n    age: number;\n}\nclass Child  extends Person {}\n\nclass Man implements Person {}<\/code><\/pre>\n\n\n\n<p>When transpiling, we will have a compilation error saying:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Class &#8216;Man&#8217; incorrectly implements interface &#8216;Person&#8217;.<\/p><p>Property &#8216;name&#8217; is missing in type &#8216;Man&#8217;.<\/p><\/blockquote>\n\n\n\n<p>And that&#8217;s because interfaces lack implementation.<br>So When we&nbsp;<code>implement<\/code>&nbsp;a class then we only take its &#8220;contract&#8221; without the implementation. In the current example, to fix the issues, we  need to do this :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Man implements Person {\n    name: string;\n    age: number;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Bottom line is that in most cases we will want to&nbsp;<code>extend<\/code>&nbsp;another class and not to&nbsp;<code>implement<\/code>&nbsp;it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the difference between&nbsp;extends&nbsp;and&nbsp;implements ? To put in simpler terms: extends: The class get all these methods and properties from the parent, so you don&#8217;t have to implement. implements: The class has to implement methods and properties. other said : extends&nbsp;means: The\u00a0new class is a child. It gets benefits coming with inheritance. It has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[331,393,395,394,372],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1412"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1412"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1412\/revisions"}],"predecessor-version":[{"id":1415,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1412\/revisions\/1415"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}