📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
TypeScript Abstraction Example
abstract class Employee {
name: string;
paymentPerHour: number;
constructor(name: string, paymentPerHour: number) {
this.name = name;
this.paymentPerHour = paymentPerHour;
}
public abstract calculateSalary(): number;
}
abstract class Employee {
name: string;
paymentPerHour: number;
constructor(name: string, paymentPerHour: number) {
this.name = name;
this.paymentPerHour = paymentPerHour;
}
public abstract calculateSalary(): number;
}
class Contractor extends Employee {
workingHours: number;
constructor(name: string, paymentPerHour: number, workingHours: number) {
super(name, paymentPerHour);
this.workingHours = workingHours;
}
calculateSalary(): number {
return this.paymentPerHour * this.workingHours;
}
}
class FullTimeEmployee extends Employee {
constructor(name: string, paymentPerHour: number) {
super(name, paymentPerHour);
}
calculateSalary(): number {
return this.paymentPerHour * 8;
}
}
let contractor: Employee;
let fullTimeEmployee: Employee;
contractor = new Contractor('Ramesh contractor', 10, 5);
fullTimeEmployee = new FullTimeEmployee('Ramesh full time employee', 8);
console.log(contractor.calculateSalary());
console.log(fullTimeEmployee.calculateSalary());
50
64
How to run the above code and see the output?
- Create a TypeScript file named "abstract-class.ts" and add the above code example to it.
- Make sure that you have installed Node.js in your machine. If not, follow this link https://nodejs.org/en/
- Make sure that you have installed TypeScript package. If you haven't installed it then install the TypeScript Package using the Node Package Manager (NPM). To begin with the installation, open any Console Window ( cmd.exe) and enter the following command in the prompt:
npm install -g typescript
- Let's run above typescript file using the following command:
c:\user\typescript-tutorial> tsc .\abstract-class.ts
var __extends = (this && this.__extends) || (function() {
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({
__proto__: []
}
instanceof Array && function(d, b) {
d.__proto__ = b;
}) ||
function(d, b) {
for (var p in b)
if (b.hasOwnProperty(p)) d[p] = b[p];
};
return extendStatics(d, b);
};
return function(d, b) {
extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Employee = /** @class */ (function() {
function Employee(name, paymentPerHour) {
this.name = name;
this.paymentPerHour = paymentPerHour;
}
return Employee;
}());
var Contractor = /** @class */ (function(_super) {
__extends(Contractor, _super);
function Contractor(name, paymentPerHour, workingHours) {
var _this = _super.call(this, name, paymentPerHour) || this;
_this.workingHours = workingHours;
return _this;
}
Contractor.prototype.calculateSalary = function() {
return this.paymentPerHour * this.workingHours;
};
return Contractor;
}(Employee));
var FullTimeEmployee = /** @class */ (function(_super) {
__extends(FullTimeEmployee, _super);
function FullTimeEmployee(name, paymentPerHour) {
return _super.call(this, name, paymentPerHour) || this;
}
FullTimeEmployee.prototype.calculateSalary = function() {
return this.paymentPerHour * 8;
};
return FullTimeEmployee;
}(Employee));
var contractor;
var fullTimeEmployee;
contractor = new Contractor('Ramesh contractor', 10, 5);
fullTimeEmployee = new FullTimeEmployee('Ramesh full time employee', 8);
console.log(contractor.calculateSalary());
console.log(fullTimeEmployee.calculateSalary());
c:\user\typescript-tutorial> node .\abstract-class.ts
50
64
Learn more about TypeScript at TypeScript Tutorial with Examples.
Comments
Post a Comment
Leave Comment