How to Implement Inheritance In Typescript?
Inheritance can be implemented in Typescript through the following:
- GO to Visual Studio, then, File -> New-> Project
- Select HTML application with Typescript as Visual C# template and name it as Inheritance.
- Now, write the code below in which a class “Student” is used to display the position of students in a class.
class Students {
constructor(public name) {}
Position(Div) {
alert(this.name + " " + "Position in the School is:" + Div);
}
}
class Student1 extends Students {
constructor(name) {
super(name);
}
Position() {
alert("Student1");
super.Position(2);
}
}
class Student2 extends Students {
constructor(name) {
super(name);
}
Position() {
alert("Student2");
super.Position(4);
}
}
var one = new Student1("Rohan")
var two: Students = new Student2("Mohan")
one.Position()
two.Position(34) - Code this command in the default htm.file
<html>
<head>
<title>Simple Inheritance In TypeScript</title>
</head>
<body >
<h1>Simple Inheritance In TypeScript</h1>
<script src="app.js"></script>
</body>
</html> - The output of this application should be like this:
- Click OK and the position of any student will be displayed
BY Best Interview Question ON 25 Jul 2020