Basic Concept In TypeScript Day 10

Lecture 10 : Basic Concept In TypeScript

Constructor :
  • A constructor is a special method which will be called whenever we create new objects. And generally used of initializing the class members. 

  • The constructor method is always defined with "constructor" keyword.

  • The members of the class in a constructor are accessed using "this" keyword.

  • Syntax :

           constructor (){
                                    //statements
                                    }

Example :

Program-
Output-
Inheritance :
  • In TypeScript, we can use common object-oriented patterns. One of the most fundamental patterns in class-based programming is being able to extend existing classes to create new ones using inheritance.

Types of Inheritance :

We can classify the inheritance into the five types :
  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance.
TypeScript supports only Single inheritance and Multilevel inheritance.It doesn't support Multiple, Hierarchical and Hybrid inheritance.

1) Single Inheritance :
  • Single inheritance can inherit properties and behavior from at most one parent class

  • It allows a derived/subclass to inherit the properties and behavior of a base class that enable the code reusability as well as we can add new features to the existing code.

  • The single inheritance makes the code less repetitive.

2) Multilevel Inheritance :
  • When a derived class is derived from another derived class, then this type of inheritance is known as multilevel inheritance

  • Thus, a multilevel inheritance has more than one parent class. 

  • It is similar to the relation between Grandfather, Father, and Child.

Example :

Program-
Access Modifiers :

There are three types of access modifiers in TypeScript are following: 

1) public :
  • By default, all members of a class in TypeScript are public. All the public members can be accessed anywhere without any restrictions.

2) private :
  • The private access modifier ensures that class members are visible only to that class and are not accessible outside the containing class.

3) protected :
  • The protected access modifier is similar to the private access modifier, except that protected members can be accessed using their deriving classes.

Example :

Program-


Comments

Popular posts from this blog

Basic Concept In C Sharp Day 16

Understanding C Sharp Day 18

Learn Garbage Collection Day 21