Exercise: Inheritance and Function Return Types
Objective:
- Understand how to use inheritance in TypeScript.
- Define and use function return types.
Instructions:
-
Create a TypeScript File:
- Create a file named
exercise5.ts
in yoursrc
directory. - Open
exercise5.ts
in your text editor and complete the following code:
- Create a file named
ts
// exercise5.ts// Define a class named 'Person' with properties: name (string) and age (number)class Person {constructor(public name: ??, public ??: number) {}// Define a method that returns a greeting message:// "Hello, my name is (name) and I am (age) years old."greet(): ??? {return ???;}}// Define a class named 'Employee' that inherits from 'Person' and adds a new property: jobTitle (string)class ?? extends ?? {constructor(name: ??, ??: number, public jobTitle: ??) {super(??, ??);}// Override the greet method to include the job title in the message:// "Hello, my name is (name), I am (age) years old and I work as a (jobTitle)."greet(): ??? {return ???;}}// Create a Person object with name: "John", age: 40let person = ??;// Create an Employee object with name: "Jane", age: 30, jobTitle: "Software Engineer"let employee = ??;// Log the result of calling the greet method on both objectsconsole.log(person.greet());console.log(employee.greet());
Check Solution
- Compile and run your code
bash
npx tscnode ./dist/exercise5.js
Your output in the console should be:
bash
> node ./dist/exercise5.jsHello, my name is John and I am 40 years old.Hello, my name is Jane, I am 30 years old and I work as a Software Engineer.
Solution:
ts
// exercise5.ts// Define a class named 'Person' with properties: name (string) and age (number)classPerson {constructor(publicname : string, publicage : number) {}// Define a method that returns a greeting message: "Hello, my name is (name) and I am (age) years old."greet (): string {return `Hello, my name is ${this.name } and I am ${this.age } years old.`;}}// Define a class named 'Employee' that inherits from 'Person' and adds a new property: jobTitle (string)classEmployee extendsPerson {constructor(name : string,age : number, publicjobTitle : string) {super(name ,age );}// Override the greet method to include the job title in the message:// "Hello, my name is (name), I am (age) years old and I work as a (jobTitle)."greet (): string {return `Hello, my name is ${this.name }, I am ${this.age } years old and I work as a ${this.jobTitle }.`;}}// Create a Person object with name: "John", age: 40letperson = newPerson ("John", 40);// Create an Employee object with name: "Jane", age: 30, jobTitle: "Software Engineer"letemployee = newEmployee ("Jane", 30, "Software Engineer");// Log the result of calling the greet method on both objectsconsole .log (person .greet ());console .log (employee .greet ());