Adding content to 108-classes

pull/3423/head
syedmouaazfarrukh 2 years ago committed by Kamran Ahmed
parent 7bac3c3444
commit 45a0b53d5f
  1. 17
      src/roadmaps/typescript/content/108-classes/100-constructor-params.md
  2. 32
      src/roadmaps/typescript/content/108-classes/101-constructor-overloading.md
  3. 16
      src/roadmaps/typescript/content/108-classes/102-access-modifiers.md
  4. 26
      src/roadmaps/typescript/content/108-classes/103-abstract-classes.md
  5. 40
      src/roadmaps/typescript/content/108-classes/104-inheritance-vs-polymorphism.md
  6. 38
      src/roadmaps/typescript/content/108-classes/105-method-overriding.md
  7. 28
      src/roadmaps/typescript/content/108-classes/index.md

@ -1 +1,16 @@
# Constructor params # 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)

@ -1 +1,31 @@
# Constructor overloading # 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)
- []()

@ -1 +1,15 @@
# Access modifiers # 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)

@ -1 +1,25 @@
# Abstract classes # 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)

@ -1 +1,39 @@
# Inheritance vs polymorphism # 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)

@ -1 +1,37 @@
# Method overriding # 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/)

@ -1 +1,29 @@
# Classes # 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)
Loading…
Cancel
Save