So far, the only type of animal we've defined is a cat (via the 'Cat' class). Our 'Cat' class also requires us to name our cats. Finally, our 'Cat' class performs rendering to the web page. However, what if we wanted to introduce other animals? How should we implement dogs, pandas, and rhinos?
While the simple answer is that we can just copy, paste, and modify our code from Cat.jspp into Dog.jspp, Panda.jspp, and Rhino.jspp, this is usually not good programming. Instead, we might notice that there is a lot of repetition in the design; all the animals need a name, all the animals need to be rendered to our web page, and all the animals can be categorized together. Therefore, we can define the behaviors we need for animals in general (such as rendering to a web page) and have all our animal classes "inherit" this common behavior. Furthermore, some animals might not need names; for example, domesticated animals like cats and dogs might need names, but we might not want to name our pandas and rhinos.
First, let's start by examining Cat.jspp. What data or behavior in Cat.jspp should be common to all animals?
From what we've defined so far, it should only be the render() method. We'll use that as the basis for understanding inheritance. Earlier, we created an Animal.jspp file. It's currently empty so open it and let's define an 'Animal' class:
external $;
module Animals
{
class Animal
{
var $element = $(
"""
<div class="animal">
<i class="icofont icofont-animal-cat"></i>
</div>
"""
);
void render() {
$("#content").append($element);
}
}
}
This works well as a basic template. However, a keen observer will notice we have a problem: the $element field will always render a cat icon. We'll get back to this later, but, first, let's change Cat.jspp to inherit from our new 'Animal' class:
external $;
module Animals
{
class Cat : Animal
{
string _name;
Cat(string name) {
_name = name;
}
}
}
We removed the $element field and render() method from Cat.jspp and we added this to the class declaration:
class Cat : Animal
The colon syntax specifies the inheritance relationship. In this case, our 'Cat' class inherits from the 'Animal' class. In other words, our 'Cat' class extends the 'Animal' class; everything that belongs to the 'Animal' class (fields, getters/setters, methods, etc) also belongs to the 'Cat' class.
In inheritance, our 'Cat' class is known as the "subclass" or "derived class" of 'Animal'. 'Animal' may be referred to as the "superclass" or "base class."
If you try to compile your code right now, you'll still see the cats rendered to the page exactly as before, but, if you hover your mouse over any of the cats, you will not see its respective name. The render() method on 'Cat' is now derived from the 'Animal' class. Since our 'Animal' class does not take names (as we want to be able to generalize the class for wild animals like pandas and rhinos), its render() method likewise does not render the HTML 'title' attribute needed to show a name on mouse over. However, we can make this behavior possible. In order to do that, it requires explanation of access modifiers, which we will now cover.
Similar Reads
Bootstrap 5 Introduction Bootstrap is a free and open-source collection of CSS and JavaScript/jQuery code used for creating dynamic websites layout and web applications. Bootstrap is one of the most popular front-end frameworks which has really a nice set of predefined CSS codes. Bootstrap uses different types of classes to
4 min read
Node.js Image Upload, Processing and Resizing using Sharp package Often in our web applications, we need to store multiple forms and formats of images, in the form of a profile picture or an image for a product in an e-commerce prototype. In most of our cases, we need to store images compressed to multiple sizes, retaining the quality. For example, for a product a
3 min read
IndexedDB | Introduction IndexedDB is a key-value database in the browser. It is a NoSQL Database. It is transactional, i.e. if a particular action falls within a transaction, none of the actions of that transaction is applied. This ensures the database remains consistent. Why use IndexedDB? The localStorage was designed fo
3 min read
JS++ | Static vs. Dynamic Polymorphism Static polymorphism is polymorphism that occurs at compile time, and dynamic polymorphism is polymorphism that occurs at runtime (during application execution). An aspect of static polymorphism is early binding. In early binding, the specific method to call is resolved at compile time. (JS++ also su
4 min read
JS++ | Subtype Polymorphism Subtyping describes type relationships, and subtype polymorphism enables operations defined for supertypes to be safely substituted with subtypes. Concretely, imagine the relation between a 'Cat' class and an 'Animal' class. (Remember: classes create data types in JS++.) In this case, within the con
3 min read
JS++ Getters and Setters In our previous example, we defined a 'setName' method that sets a class 'name' field we declared. A method whose sole responsibility is to write to or modify a class field is known as a "setter" or "setter method." Conversely, a method whose sole responsibility is to return the current data of a cl
4 min read
How to Scrape a Website Using Puppeteer in Node.js ? Puppeteer is a Node.js library that provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It allows automating, testing, and scraping of web pages over a headless/headful browser. Installing Puppeteer: To use Puppeteer, you must have Node.js installed. Then, Pu
2 min read
JS++ | Access Modifiers and 'super' Access modifiers allow us to change the "visibility" and "access privileges" of a class (or module) member. These are best understood by example. JS++ has three access modifiers: private, protected, and public. A private member is the least permissive. If a member is declared as 'private' it can onl
15 min read
JS++ | Types in JavaScript In this chapter, we're going to explore JavaScript programming styles and how developers worked with types in JavaScript (rather than JS++). This chapter will help you understand the next chapters which explain the JS++ type system in detail. In this tutorial, we will be using the Google Chrome web
10 min read
JS++ | Upcasting and Downcasting Now that we understand both subtyping and static versus dynamic polymorphism, we can learn about upcasting and downcasting. Upcasting and downcasting is based on type relationships. In other words, if you have data of type 'Animal', you can "downcast" it to its subtype 'Dog'. Conversely, if you have
3 min read