Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8744093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:47:25+00:00 2026-06-13T11:47:25+00:00

See the inheritance example from the playground on the TypeScript site: class Animal {

  • 0

See the inheritance example from the playground on the TypeScript site:

class Animal {
  public name;
  constructor(name) {
    this.name = name;
  }
  move(meters) {
    alert(this.name + " moved " + meters + "m.");
  }
}

class Snake extends Animal {
  constructor(name) {
    super(name);
  }
  move() {
    alert("Slithering...");
    super.move(5);
  }
}

class Horse extends Animal {
  constructor(name) {
    super(name);
  }
  move() {
    alert(super.name + " is Galloping...");
    super.move(45);
  }
}

var sam = new Snake("Sammy the Python");
var tom: Animal = new Horse("Tommy the Palomino");

sam.move();
tom.move(34);

I have changed one line of code: the alert in Horse.move(). There I want to access super.name, but that returns just undefined. IntelliSense is suggesting that I can use it and TypeScript compiles fine, but it does not work.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:47:26+00:00Added an answer on June 13, 2026 at 11:47 am

    Working example. Notes below.

    class Animal {
        constructor(public name) {
        }
    
        move(meters) {
            alert(this.name + " moved " + meters + "m.");
        }
    }
    
    class Snake extends Animal {
        move() {
            alert(this.name + " is Slithering...");
            super.move(5);
        }
    }
    
    class Horse extends Animal {
        move() {
            alert(this.name + " is Galloping...");
            super.move(45);
        }
    }
    
    var sam = new Snake("Sammy the Python");
    var tom: Animal = new Horse("Tommy the Palomino");
    
    sam.move();
    tom.move(34);
    
    1. You don’t need to manually assign the name to a public variable. Using public name in the constructor definition does this for you.

    2. You don’t need to call super(name) from the specialised classes.

    3. Using this.name works.

    Notes on use of super.

    This is covered in more detail in section 4.9.2 of the language specification.

    The behaviour of the classes inheriting from Animal is not dissimilar to the behaviour in other languages. You need to specify the super keyword in order to avoid confusion between a specialised function and the base class function. For example, if you called move() or this.move() you would be dealing with the specialised Snake or Horse function, so using super.move() explicitly calls the base class function.

    There is no confusion of properties, as they are the properties of the instance. There is no difference between super.name and this.name – there is simply this.name. Otherwise you could create a Horse that had different names depending on whether you were in the specialized class or the base class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read the decorator design pattern from Wikipedia, and code example from this site
I am experimenting with JavaScript Inheritance. Basically, I am following this tutorial. I see
For example, I am using multi-table inheritance for a class Node with sub-classes ConceptNode
I am new to this website and I am trying a simple inheritance example
I use SINGLE_TABLE inheritance startegy to map my usres (see code example bellow). Is
In the trivial example inheritance hierarchy: class Food { virtual ~Food(); }; class Fruit
See the picture below. It's a flash game from a well known website :)
See this question. How to show popup message like in stackoverflow I am trying
I've seen this question regarding inheritance in Backbone: Backbone.js view inheritance . Useful but
To follow from my previous question about virtual and multiple inheritance (in a cross

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.