diff --git a/src/roadmaps/typescript/content/108-classes/100-constructor-params.md b/src/roadmaps/typescript/content/108-classes/100-constructor-params.md index c5dbb00db..e912d7317 100644 --- a/src/roadmaps/typescript/content/108-classes/100-constructor-params.md +++ b/src/roadmaps/typescript/content/108-classes/100-constructor-params.md @@ -1 +1,16 @@ -# Constructor params \ No newline at end of file +# Constructor Params + +In TypeScript, constructor parameters can be declared with access modifiers (e.g. public, private, protected) and/or type annotations. The parameters are then automatically assigned to properties of the same name within the constructor, and can be accessed within the class. For example: + + ``` + class Example { + constructor(private name: string, public age: number) {} + } + ``` + +In this example, the constructor has two parameters: name and age. name has a private access modifier, so it can only be accessed within the Example class. age has a public access modifier, so it can be accessed from outside the class as well. + +Learn more from the following links: + +- [TypeScript - Construct](https://www.typescriptlang.org/docs/handbook/2/classes.html#constructors) +- [TypeScript - Methods and constructors](https://www.youtube.com/watch?v=d9IJyMOmJoE) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/108-classes/101-constructor-overloading.md b/src/roadmaps/typescript/content/108-classes/101-constructor-overloading.md index ba4a68a4c..196356921 100644 --- a/src/roadmaps/typescript/content/108-classes/101-constructor-overloading.md +++ b/src/roadmaps/typescript/content/108-classes/101-constructor-overloading.md @@ -1 +1,31 @@ -# Constructor overloading \ No newline at end of file +# Constructor Overloading + +In TypeScript, you can achieve constructor overloading by using multiple constructors with different parameter lists in a single class. When you create an instance of the class, the constructor with the matching parameter list is called. Here's an example: + + ``` + class MyClass { + property1: number; + property2: string; + + constructor(property1: number) { + this.property1 = property1; + } + + constructor(property1: number, property2: string) { + this.property1 = property1; + this.property2 = property2; + } + } + ``` + +In this example, we have two constructors with different parameter lists: constructor(property1: number) and constructor(property1: number, property2: string). When you create an instance of the class, the constructor with the matching parameter list is called: + +``` +let myInstance1 = new MyClass(10); +let myInstance2 = new MyClass(10, "Hello"); +``` + +Learn more from the following resources: + +- [Constructors - TypeScript](https://www.typescriptlang.org/docs/handbook/2/classes.html#constructors) +- []() \ No newline at end of file diff --git a/src/roadmaps/typescript/content/108-classes/102-access-modifiers.md b/src/roadmaps/typescript/content/108-classes/102-access-modifiers.md index c0c282eef..a61abe912 100644 --- a/src/roadmaps/typescript/content/108-classes/102-access-modifiers.md +++ b/src/roadmaps/typescript/content/108-classes/102-access-modifiers.md @@ -1 +1,15 @@ -# Access modifiers \ No newline at end of file +# Access Modifiers + +In TypeScript, access modifiers are keywords used to control the visibility and accessibility of class properties and methods. There are three access modifiers in TypeScript: + +1. `Public:` This is the default access modifier. Properties and methods declared as public can be accessed from anywhere, both inside and outside the class. +2. `Private:` Properties and methods declared as private can only be accessed within the same class. They are not accessible from outside the class. +3. `Protected:` Properties and methods declared as protected can be accessed within the class and its subclasses. They are not accessible from outside the class and its subclasses. + +Access modifiers in TypeScript allow you to define the level of visibility and accessibility of properties and methods in your class, making your code more maintainable and secure. + + +Learn more from the following resources: + +- [TypeScript Access Modifiers](https://www.typescripttutorial.net/typescript-tutorial/typescript-access-modifiers/) +- [TypeScript - Data Modifiers](https://www.tutorialsteacher.com/typescript/data-modifiers) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/108-classes/103-abstract-classes.md b/src/roadmaps/typescript/content/108-classes/103-abstract-classes.md index afa58dfc0..ca08f2fb7 100644 --- a/src/roadmaps/typescript/content/108-classes/103-abstract-classes.md +++ b/src/roadmaps/typescript/content/108-classes/103-abstract-classes.md @@ -1 +1,25 @@ -# Abstract classes \ No newline at end of file +# Abstract Classes + +Abstract classes in TypeScript are classes that cannot be instantiated on their own and must be subclassed by other classes. Abstract classes provide a blueprint for other classes and can have abstract methods, which are methods without a body and must be overridden by the subclass. These classes are useful for defining a common interface or basic functionality that other classes can inherit and build upon. + +For example: + +``` +abstract class Animal { + abstract makeSound(): void; + move(): void { + console.log('moving...'); + } +} + +class Dog extends Animal { + makeSound(): void { + console.log('bark'); + } +} +``` + +Learn more from the following resources: + +- [Abstract Classes](https://www.typescriptlang.org/docs/handbook/classes.html#abstract-classes) +- [TypeScript - Abstract Class](https://www.tutorialsteacher.com/typescript/abstract-class) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/108-classes/104-inheritance-vs-polymorphism.md b/src/roadmaps/typescript/content/108-classes/104-inheritance-vs-polymorphism.md index da7420564..e18b9f470 100644 --- a/src/roadmaps/typescript/content/108-classes/104-inheritance-vs-polymorphism.md +++ b/src/roadmaps/typescript/content/108-classes/104-inheritance-vs-polymorphism.md @@ -1 +1,39 @@ -# Inheritance vs polymorphism \ No newline at end of file +# Inheritance vs Polymorphism + +Inheritance and polymorphism are two fundamental concepts in object-oriented programming, and they are supported in TypeScript as well. + +Inheritance refers to a mechanism where a subclass inherits properties and methods from its parent class. This allows a subclass to reuse the code and behavior of its parent class while also adding or modifying its own behavior. In TypeScript, inheritance is achieved using the extends keyword. + +Polymorphism refers to the ability of an object to take on many forms. This allows objects of different classes to be treated as objects of a common class, as long as they share a common interface or inheritance hierarchy. In TypeScript, polymorphism is achieved through method overriding and method overloading. + +For example: + + ``` + class Animal { + makeSound(): void { + console.log('Making animal sound'); + } + } + + class Dog extends Animal { + makeSound(): void { + console.log('Bark'); + } + } + + class Cat extends Animal { + makeSound(): void { + console.log('Meow'); + } + } + + let animal: Animal; + animal = new Dog(); + animal.makeSound(); // Output: Bark + animal = new Cat(); + animal.makeSound(); // Output: Meow + ``` + +Learn more from the following resources: + +- [Inheritance and Polymorphism In TypeScript](https://www.youtube.com/watch?v=Sn6K57YSuwU) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/108-classes/105-method-overriding.md b/src/roadmaps/typescript/content/108-classes/105-method-overriding.md index 089e66cbe..8284fe882 100644 --- a/src/roadmaps/typescript/content/108-classes/105-method-overriding.md +++ b/src/roadmaps/typescript/content/108-classes/105-method-overriding.md @@ -1 +1,37 @@ -# Method overriding \ No newline at end of file +# Method Overriding + +In TypeScript, method overriding is a mechanism where a subclass provides a new implementation for a method that is already defined in its parent class. This allows the subclass to inherit the behavior of the parent class, but change its behavior to fit its own needs. + +To override a method in TypeScript, you need to use the override keyword, and the signature of the method in the subclass must match exactly with the signature of the method in the parent class. + +For example: + + ``` + class Animal { + makeSound(): void { + console.log('Making animal sound'); + } + } + + class Dog extends Animal { + makeSound(): void { + console.log('Bark'); + } + } + + let animal: Animal; + animal = new Dog(); + animal.makeSound(); // Output: Bark + ``` + +In this example, the `Dog` class overrides the makeSound method defined in the Animal class and provides its own implementation. When the `makeSound` method is called on an instance of the `Dog` class, it will use the implementation in the `Dog` class rather than the implementation in the `Animal` class. + + + + + + +Learn more from the following resources: + +- [TypeScript - Overriding Methods](https://www.typescriptlang.org/docs/handbook/2/classes.html#overriding-methods) +- [Method Overriding in TypeScript](https://www.geeksforgeeks.org/method-overriding-in-typescript/) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/108-classes/index.md b/src/roadmaps/typescript/content/108-classes/index.md index f4ae2a543..329934005 100644 --- a/src/roadmaps/typescript/content/108-classes/index.md +++ b/src/roadmaps/typescript/content/108-classes/index.md @@ -1 +1,29 @@ -# Classes \ No newline at end of file +# Classes + +Classes in TypeScript are a blueprint for creating objects (instances of a class), providing a way to structure objects and encapsulate data and behavior. Classes in TypeScript have a similar syntax to classes in other object-oriented programming languages, such as Java and C#. + +A class in TypeScript is defined using the class keyword, followed by the name of the class. The class definition can include fields (also known as properties or attributes), methods (functions), and a constructor. + +For example: + + ``` + class Animal { + name: string; + constructor(name: string) { + this.name = name; + } + makeSound(): void { + console.log(`${this.name} is making a sound`); + } + } + + const dog = new Animal('Dog'); + dog.makeSound(); // Output: Dog is making a sound + ``` + +In this example, the `Animal` class has a name field, a constructor that sets the value of the `name` field, and a `makeSound` method. An instance of the `Animal` class can be created using the `new` keyword, and its methods and properties can be accessed using dot notation. + +Learn more from the following resources: + +- [Tutorial - Classes](https://www.typescriptlang.org/docs/handbook/2/classes.html) +- [TypeScript Tutorial - Classes](https://www.youtube.com/watch?v=OsFwOzr3_sE) \ No newline at end of file