📘 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.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Note: To run or execute source code examples of this tutorial, follow How to Run the TypeScript Code guide.
Table of contents
- TypeScript Class Example
- TypeScript constructor
- Creating an Object of Class
- Inheritance in TypeScript
- Public, private, and protected modifiers
- Readonly modifier
- Accessors - getters/setters
- Static Properties
- Abstract Classes
TypeScript Simple Class Example
- id
- firstName
- lastName
- getFullName()
class Employee {
id: number;
firstName: string;
lastName: string;
constructor(id: number, firstName: string, lastName: string) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
getFullName() {
return this.firstName + ' ' + this.lastName;
}
}
// create Employee class object
let employee = new Employee(100, 'Ramesh', 'Fadatare');
console.log(employee);
console.log(employee.getFullName());
Employee { id: 100, firstName: 'Ramesh', lastName: 'Fadatare' }
Ramesh Fadatare
"use strict";
exports.__esModule = true;
var Employee = /** @class */ (function () {
function Employee(id, firstName, lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
Employee.prototype.getFullName = function () {
return this.firstName + this.lastName;
};
return Employee;
}());
exports.Employee = Employee;
// create Employee class object
var employee = new Employee(100, 'Ramesh', 'Fadatare');
console.log(employee);
console.log(employee.getFullName());
Constructor
Example: Constructor
class Employee {
id: number;
firstName: string;
lastName: string;
constructor(id: number, firstName: string, lastName: string) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
}
class Employee{
id: number;
firstName: string;
lastName: string;
getFullName(){
return this.firstName + ' ' + this.lastName;
}
}
// create Employee class object
let employee = new Employee();
employee.id = 100;
employee.firstName = 'Ramesh';
employee.lastName = 'Fadatare';
console.log(employee);
console.log(employee.getFullName());
Employee { id: 100, firstName: 'Ramesh', lastName: 'Fadatare' }
Ramesh Fadatare
Creating an Object of Class
Example: Create an Object
class Employee{
id: number;
firstName: string;
lastName: string;
}
// create Employee class object
let employee = new Employee();
employee.id = 100;
employee.firstName = 'Ramesh';
employee.lastName = 'Fadatare';
console.log(employee);
class Employee{
id: number;
firstName: string;
lastName: string;
constructor(id: number, firstName: string, lastName: string){
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
getFullName(){
return this.firstName + ' ' + this.lastName;
}
}
// create Employee class object
let employee = new Employee(100, 'Ramesh', 'Fadatare');
console.log(employee);
console.log(employee.getFullName());
Inheritance
class Animal {
move(distanceInMeters: number = 0) {
console.log(`Animal moved ${distanceInMeters}m.`);
}
}
class Dog extends Animal {
bark() {
console.log('Woof! Woof!');
}
}
const dog = new Dog();
dog.bark();
dog.move(10);
dog.bark();
class Animal {
name: string;
constructor(theName: string) { this.name = theName; }
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Snake extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = 5) {
console.log("Slithering...");
super.move(distanceInMeters);
}
}
class Horse extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = 45) {
console.log("Galloping...");
super.move(distanceInMeters);
}
}
let sam = new Snake("Sammy the Python");
let tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
Public, private, and protected modifiers
Public by default
class Employee {
public id: number;
public firstName: string;
public lastName: string;
constructor(id: number, firstName: string, lastName: string) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public getFullName() {
return this.firstName + ' ' + this.lastName;
}
}
// create Employee class object
let employee = new Employee(100, 'Ramesh', 'Fadatare');
console.log(employee);
console.log(employee.getFullName());
Employee { id: 100, firstName: 'Ramesh', lastName: 'Fadatare' }
Ramesh Fadatare
var Employee = /** @class */ (function () {
function Employee(id, firstName, lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
Employee.prototype.getFullName = function () {
return this.firstName + ' ' + this.lastName;
};
return Employee;
}());
// create Employee class object
var employee = new Employee(100, 'Ramesh', 'Fadatare');
console.log(employee);
console.log(employee.getFullName())
Understanding private
class Employee {
private id: number;
public firstName: string;
public lastName: string;
public getFullName() {
return this.firstName + ' ' + this.lastName;
}
}
// create Employee class object
let employee = new Employee();
employee.id = 100; // Property 'id' is private and only accessible within class 'Employee'.ts
employee.firstName = 'Ramesh';
console.log(employee);
console.log(employee.getFullName());
Property 'id' is private and only accessible within class 'Employee'.ts
Understanding protected
class Person {
protected name: string;
constructor(name: string) {
this.name = name;
}
}
class Employee extends Person {
private department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
public getElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}
let howard = new Employee("Howard", "Sales");
console.log(howard.getElevatorPitch());
console.log(howard.name); // Property 'name' is protected and only
// accessible within class 'Person' and its subclasses.
Property 'name' is protected and only accessible within class 'Person' and its subclasses.
class Person {
protected name: string;
protected constructor(theName: string) { this.name = theName; }
}
// Employee can extend Person
class Employee extends Person {
private department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
public getElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}
let howard = new Employee("Howard", "Sales");
let john = new Person("John"); // Error: The 'Person' constructor is protected
Readonly modifier
class Employee{
readonly id: number;
public firstName: string;
readonly lastName: string;
constructor(id: number, firstName: string, lastName: string){
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public getFullName(){
return this.firstName + ' ' + this.lastName;
}
}
// create Employee class object
let employee = new Employee(100, 'Ramesh', 'Fadatare');
employee.id = 200; // Error: Cannot assign to 'id' because it is a read-only property.
employee.lastName = 'Kapoor'; // Error: Cannot assign to 'lastName' because it is a read-only property
console.log(employee);
console.log(employee.getFullName());
employee.id = 200; // Error: Cannot assign to 'id' because it is a read-only property.
employee.lastName = 'Kapoor'; // Error: Cannot assign to 'lastName' because it is a read-only property
Accessors - getters/setters
class Employee {
private _id: number;
private _fullName: string;
public get id(): number {
return this._id;
}
public set id(value: number) {
this._id = value;
}
public get fullName(): string {
return this._fullName;
}
public set fullName(value: string) {
this._fullName = value;
}
}
// create Employee class object
let employee = new Employee();
employee.id = 200;
employee.fullName = 'Ramesh Fadatare';
console.log(employee);
console.log(employee.fullName);
Employee { _id: 200, _fullName: 'Ramesh Fadatare' }
Ramesh Fadatare
var Employee = /** @class */ (function () {
function Employee() {
}
Object.defineProperty(Employee.prototype, "id", {
get: function () {
return this._id;
},
set: function (value) {
this._id = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Employee.prototype, "fullName", {
get: function () {
return this._fullName;
},
set: function (value) {
this._fullName = value;
},
enumerable: true,
configurable: true
});
return Employee;
}());
// create Employee class object
var employee = new Employee();
employee.id = 200;
employee.fullName = 'Ramesh Fadatare';
console.log(employee);
console.log(employee.fullName);
Static Properties
class Employee{
static fullName: string;
static getFullName(){
return Employee.fullName;
}
}
// create Employee class object
Employee.fullName = 'Ramesh Fadatare';
console.log(Employee.getFullName());
Ramesh Fadatare
Abstract Classes
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
Learn more about TypeScript at TypeScript Tutorial with Examples.
Comments
Post a Comment
Leave Comment