{"id":1475,"date":"2018-10-30T13:54:48","date_gmt":"2018-10-30T13:54:48","guid":{"rendered":"https:\/\/www.courses.tegabrain.com\/cc18\/?page_id=1475"},"modified":"2018-10-30T16:39:36","modified_gmt":"2018-10-30T16:39:36","slug":"inheritance-and-polymorphism-in-p5js","status":"publish","type":"page","link":"https:\/\/www.courses.tegabrain.com\/cc18\/inheritance-and-polymorphism-in-p5js\/","title":{"rendered":"Inheritance and polymorphism in p5JS"},"content":{"rendered":"<p>You may have encountered the terms <em>inheritance<\/em> and <em>polymorphism<\/em> in your programming life before this book. After all, they are two of the three fundamental principles behind the theory of object-oriented programming (the other being <em>encapsulation<\/em>). This is covered in other Processing or Java programming books too. The text, <em>Learning Processing<\/em>, has close to an entire chapter (#22) dedicated to these two topics.<\/p>\n<h3>Inheritance basics.<\/h3>\n<p>Let\u2019s take a different example, the world of animals: dogs, cats, monkeys, pandas, wombats, and sea nettles. We\u2019ll start by programming a <span class=\"klass\">Dog<\/span> class. A <span class=\"klass\">Dog<\/span> object will have an age variable (an integer), as well as <span class=\"function\">eat()<\/span>, <span class=\"function\">sleep()<\/span>, and <span class=\"function\">bark()<\/span> functions.<\/p>\n<div class=\"source-code\">\n<div class=\"code-block\">\n<div class=\"code-comment-pair no-comment\">\n<pre><span class=\"one-line\"><span class=\"kd\">class<\/span> <span class=\"nc\">Dog<\/span> <span class=\"o\">{<\/span><\/span>\r\n<span class=\"one-line\"><span class=\"o\"><span class=\"n\">\r\n   contructor<\/span>() {  \r\n<span class=\"n\">    this.age<\/span> = <span class=\"mi\">0<\/span>;  \r\n   } \r\n \r\n<span class=\"nf\">   eat<\/span>() {  \r\n<span class=\"n\">    println<\/span>(<span class=\"s\">\"Yum!\"<\/span>);  \r\n   }  \r\n<span class=\"nf\">\r\n   sleep<\/span>() {  \r\n<span class=\"n\">   println<\/span>(<span class=\"s\">\"Zzzzzz\"<\/span>);  \r\n   }\r\n\r\n<span class=\"nf\">   bark<\/span>() {  \r\n<span class=\"n\">    println<\/span>(<span class=\"s\">\"WOOF!\"<\/span>);  \r\n   }\r\n  }\r\n\r\n<\/span><\/span><\/pre>\n<\/div>\n<div class=\"code-comment-pair stretch\">\n<div class=\"code-comment stretch\">Dogs and cats have the same variables (age) and functions (eat, sleep).<\/div>\n<\/div>\n<div class=\"code-comment-pair stretch\">\n<div class=\"code-comment stretch\">A unique function for barking.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Now, let\u2019s move on to cats.<\/p>\n<div class=\"source-code\">\n<div class=\"code-block\">\n<div class=\"code-comment-pair no-comment\">\n<pre><span class=\"one-line\"><span class=\"kd\">class<\/span> <span class=\"nc\">Cat<\/span> <span class=\"o\">{\r\n<\/span><\/span>\r\n<span class=\"one-line\">  <span class=\"n\">constructor<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/span>\r\n<span class=\"one-line\">    this.<span class=\"n\">age<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"o\">;<\/span><\/span>\r\n<span class=\"one-line\">  <span class=\"o\">}<\/span><\/span>\r\n \r\n<span class=\"one-line\">  <span class=\"nf\">eat<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/span>\r\n<span class=\"one-line\">    <span class=\"n\">println<\/span><span class=\"o\">(<\/span><span class=\"s\">\"Yum!\"<\/span><span class=\"o\">);<\/span><\/span>\r\n<span class=\"one-line\">  <span class=\"o\">}<\/span><\/span>\r\n \r\n<span class=\"one-line\">  <span class=\"nf\">sleep<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/span>\r\n<span class=\"one-line\">    <span class=\"n\">println<\/span><span class=\"o\">(<\/span><span class=\"s\">\"Zzzzzz\"<\/span><span class=\"o\">);<\/span><\/span>\r\n<span class=\"one-line\">  <span class=\"o\">}<\/span><\/span>\r\n \r\n<span class=\"one-line\">  <span class=\"nf\">meow<\/span><span class=\"o\">()<\/span> <span class=\"o\">{<\/span><\/span>\r\n<span class=\"one-line\">    <span class=\"n\">println<\/span><span class=\"o\">(<\/span><span class=\"s\">\"MEOW!\"<\/span><span class=\"o\">);<\/span><\/span>\r\n<span class=\"one-line\">  <span class=\"o\">}<\/span><\/span>\r\n<span class=\"one-line\"><span class=\"o\">}<\/span><\/span><\/pre>\n<\/div>\n<\/div>\n<div><\/div>\n<\/div>\n<p>As we rewrite the same code for fish, horses, koalas, and lemurs, this process will become rather tedious. Instead, let\u2019s develop a generic <span class=\"klass\">Animal<\/span> class that can describe any type of animal. All animals eat and sleep, after all. We could then say:<\/p>\n<div class=\"list\">\n<ul>\n<li>A dog is an animal and has all the properties of animals and can do all the things animals do. Also, a dog can bark.<\/li>\n<li>A cat is an animal and has all the properties of animals and can do all the things animals do. Also, a cat can meow.<\/li>\n<\/ul>\n<\/div>\n<p>Inheritance makes this all possible. With inheritance, classes can inherit properties (variables) and functionality (methods) from other classes. A <span class=\"klass\">Dog<\/span> class is a child (<strong><em>subclass<\/em><\/strong>) of an <span class=\"klass\">Animal<\/span> class. Children will automatically inherit all variables and functions from the parent (<strong><em>superclass<\/em><\/strong>), but can also include functions and variables not found in the parent. Like a phylogenetic &#8220;tree of life,&#8221; inheritance follows a tree structure. Dogs inherit from canines, which inherit from mammals, which inherit from animals, etc.<\/p>\n<div class=\"image-container \">\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/natureofcode.com\/book\/imgs\/chapter04\/ch04_02.png?w=1140&#038;ssl=1\" alt=\"Nature of Code Image\" \/><\/p>\n<p class=\"caption\">Figure 4.2<\/p>\n<\/div>\n<p>Here is how the syntax works with inheritance.<\/p>\n<div class=\"source-code\">\n<div class=\"code-block\">\n<div class=\"code-comment-pair \">\n<div class=\"code-comment \">The Animal class is the parent (or super) class.<\/div>\n<pre lang=\"java\">\/\/animal class is the parent class\r\n\r\nclass Animal { \r\n\r\n\/\/Dog and Cat inherit the variable age.\r\n\r\n  constructor() {\r\n    this.age = 0; \r\n  }\r\n\r\n\/\/Dog and Cat inherit the functions eat() and sleep().\r\n\r\n  eat() {\r\n    println(\"Yum!\");\r\n  }\r\n \r\n  sleep() {\r\n    println(\"Zzzzzz\");\r\n  }\r\n}\r\n \r\n\r\n\/\/The Dog class is the child (or sub) class, indicated by the code \"extends Animal\".\r\n\r\nclass Dog extends Animal {\r\n  constructor() {\r\n    super(); \/\/super() executes code found in the parent class.\r\n  }\r\n\r\n\/\/We define bark() in the child class, since it isn't part of the parent class.\r\n  bark() {\r\n    println(\"WOOF!\");\r\n  }\r\n}\r\n \r\nclass Cat extends Animal {\r\n  constructor() {\r\n    super();\r\n  }\r\n\r\n  meow() {\r\n    println(\"MEOW!\");\r\n  }\r\n}\r\n<\/pre>\n<div class=\"code-comment \">This brings up two new terms:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"list\">\n<ul>\n<li><strong><em>extends<\/em><\/strong> \u2013 This keyword is used to indicate a parent for the class being defined. Note that classes can only extend <em>one<\/em> class. However, classes can extend classes that extend other classes, i.e. <span class=\"klass\">Dog extends Animal<\/span>, <span class=\"klass\">Terrier extends Dog<\/span>. Everything is inherited all the way down the line.<\/li>\n<li><strong><em>super()<\/em><\/strong> \u2013 This calls the constructor in the parent class. In other words, whatever you do in the parent constructor, do so in the child constructor as well. Other code can be written into the constructor in addition to <span class=\"function\">super()<\/span>. <span class=\"function\">super()<\/span> can also receive arguments if there is a parent constructor defined with matching arguments.<\/li>\n<\/ul>\n<\/div>\n<p>A subclass can be expanded to include additional functions and properties beyond what is contained in the superclass. For example, let\u2019s assume that a <span class=\"klass\">Dog<\/span> object has a haircolor variable in addition to age, which is set randomly in the constructor. The class would now look like this:<\/p>\n<pre lang=\"java\">\/\/A child class can introduce new variables not included in the parent.\r\nclass Dog extends Animal { \r\n  constructor() {\r\n    super();\r\n    this.haircolor = color(random(255));\r\n  }\r\n \r\n  bark() {\r\n    println(\"WOOF!\");\r\n  }\r\n}\r\n\r\n<\/pre>\n<p>Note how the parent constructor is called via <span class=\"function\">super()<\/span>, which sets the age to 0, but the haircolor is set inside the <span class=\"klass\">Dog<\/span> constructor itself. If a <span class=\"klass\">Dog<\/span> object eats differently than a generic <span class=\"klass\">Animal<\/span> object, parent functions can be <em>overridden<\/em> by rewriting the function inside the subclass.<\/p>\n<pre lang=\"java\">class Dog extends Animal {\r\n \r\n  constructor() {\r\n     super();\r\n     this.haircolor = color(random(255));\r\n  }\r\n \r\n\r\n\/\/A child can override a parent function if necessary.\r\n\r\n  eat() {\r\n\r\n\/\/A Dog's specific eating characteristics\r\n\r\n    println(\"Woof! Woof! Slurp.\")\r\n  }\r\n \r\n  bark() {\r\n    println(\"WOOF!\");\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>But what if a dog eats the same way as a generic animal, just with some extra functionality? A subclass can both run the code from a parent class and incorporate custom code.<\/p>\n<pre lang=\"java\">class Dog extends Animal {\r\n\r\n   constructor() {\r\n     super();\r\n     this.haircolor = color(random(255));\r\n   }\r\n \r\n   eat() {\r\n\r\n\/\/Call eat() from Animal. A child can execute a function from the parent \r\n\/\/while adding its own code.\r\n\r\n     super.eat();\r\n\r\n\/\/Add some additional code for a Dog's specific eating characteristics.\r\n\r\n     println(\"Woof!!!\");\r\n   }\r\n \r\n   bark() {\r\n    println(\"WOOF!\");\r\n  }\r\n}\r\n\r\n\r\n<\/pre>\n<p>Finally, here is the example where the parent constructor requires an argument.<\/p>\n<pre lang=\"java\">class Animal { \r\n  constructor(name) {\r\n    this.name = name;\r\n  }\r\n  \r\n  speak() {\r\n    print(this.name + ' makes a noise.');\r\n  }\r\n}\r\n\r\nclass Dog extends Animal {\r\n  constructor(name) {\r\n    super(name); \/\/ call the super class constructor and pass in the name parameter\r\n  }\r\n\r\n  speak() {\r\n    print(this.name + ' barks.');\r\n  }\r\n}\r\n\r\n\/\/In your program call the class to make a dog object d.\r\n\/\/Then call its functions!\r\n\r\nvar d = new Dog('Mitzie');\r\nd.speak(); \/\/ Mitzie barks.\r\n\r\n<\/pre>\n<p>Tutorial is taken from Nature of Code by Daniel Shiffman (https:\/\/natureofcode.com\/)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may have encountered the terms inheritance and polymorphism in your programming life before this book. After all, they are two of the three fundamental principles behind the theory of object-oriented programming (the other being encapsulation). This is covered in other Processing or Java programming books too. The text, Learning Processing, has close to an<a class=\"more-link button\" href=\"https:\/\/www.courses.tegabrain.com\/cc18\/inheritance-and-polymorphism-in-p5js\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-1475","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/Paeb9f-nN","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.courses.tegabrain.com\/cc18\/wp-json\/wp\/v2\/pages\/1475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.courses.tegabrain.com\/cc18\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.courses.tegabrain.com\/cc18\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.courses.tegabrain.com\/cc18\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.courses.tegabrain.com\/cc18\/wp-json\/wp\/v2\/comments?post=1475"}],"version-history":[{"count":8,"href":"https:\/\/www.courses.tegabrain.com\/cc18\/wp-json\/wp\/v2\/pages\/1475\/revisions"}],"predecessor-version":[{"id":1507,"href":"https:\/\/www.courses.tegabrain.com\/cc18\/wp-json\/wp\/v2\/pages\/1475\/revisions\/1507"}],"wp:attachment":[{"href":"https:\/\/www.courses.tegabrain.com\/cc18\/wp-json\/wp\/v2\/media?parent=1475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}