Nemo Teaches Us About Pseudoclassical Subclassing

I was really excited about using Nemo to explain how pseudoclassical subclassing works, but the example has become somewhat morbid as Nemo ends up becoming dinner by the end of of the chain. So bear with me…

Super Class: Fish

Here I have a constructor for our every day fish. It takes in a type and a speed(mph) as its parameters. Regular fish also have the ability to swim, so I’ve also included a method called SwimFaster that adds 5mph to the fish’s speed. If you need help with how the pseudoclassical instantiation patterns, please refer to my post about Instantiation Patterns at the FroYo shop.

Subclass: Disney Fish

I had a Finding Nemo/Finding Dory movie marathon the other day and decided to make a constructor for the fish I saw in the movie. The class is called “DisneyFish.” Since DisneyFish are also real fish, I can make it a subclass of Fish by using the Fish methods and properties in my DisneyFish constructor. I do this by “calling” those methods into my constructor using .call. This allows the “this” that is created with every new instance of DisneyFish to be connected to that particular instance and not to the Fish super class. This is very confusing, but take a look at the No-No examples at the bottom of the page for some more clarification.

Nemo and Dory are unique fish that can talk and make friends so they get a special property and methods on their prototype that the regular fish do not have. Because the DinnerFish.prototype inherits its methods from Fish.prototype, DisneyFish also have the swimFaster method. Lastly, we need to reassign the constructor to itself so it doesn’t continue to refer to Fish.prototype and we do that by typing in this: DisneyFish.prototype.constructor = DisneyFish;

Subclass: Dinner Fish

Continuing down the chain of fish is the dinnerFish. These fish are also regular ol’ fish, but instead of swimming in the sea, they are resting on a baking dish in my oven. Unfortunately for this example, that fish used to be a DisneyFish named Nemo.

I have used .call to pull in the properties and methods from DisneyFish into my dinnerFish constructor because I want my dinnerFish to have the extra property of “friend” (I could have also created dinnerFish using the Fish superclass, but it wouldn’t have the extra properties that I want from disneyFish).

When I refer to the swimFaster method, the interpreter checks DisneyFish first before referring to the regular Fish superclass. The dinnerFish also has one additional method called caught. When a fish is caught, its speed is reduced to zero. However, the moment before it was caught, it was lost and we want “help” to be alerted. In order to to do this, I call getLost from DisneyFish so that when caught is run, the other function getLost runs and we are alerted with “Help!”. If I didn’t want the alert to pop up, I could eliminate the line that calls the getLost method and the caught method would only adjust the mph to zero without alerting anything.

Here are three examples of how NOT to make a subclass. It can get very confusing with what “this” refers to in pseudoclassical subclasses, but hopefully the pictures will help eliminate some of the confusing as to why you can’t call new Fish within the subclass constructor and why you must use the .call method.

A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.