Abstract Classes , Need of Interfaces and its Differences

    12th Day                                                                                                

    Hey Guys ,I hope that you are enjoying with my blogs writing and today's we                            will cover some concepts like:

                                        


         ABSTACT CLASSES :
  • A TypeScript Abstract class is a class which may have some unimplemented methods. These methods are called abstract methods. We can't create an instance of an abstract class. But other classes can derived from abstract class and reuse the functionality of base class.
  • SYNTAX:
        export abstract class Employee
{ // This is the half defined parent class
     name:string ="";
     designation:string ="";
     abstract Dowork();
}

Explanation: In above ,We have created an abstract class Employee. And first
method Dowork is an abstract and we have used abstract keyword before the method .
  •  Semantically, Abstract class means half define parents class and we can also say that Employee class is a  generalization class but not in all conditions. 
  • Remember that, Parent class and generalization class has some difference, it has not each and every condition same. 
  •  In some parent classes has all method  implemented and  it has not any half define class in that case it is not a generalization class.

Derived Class: It is also called specialization class  or concrete class because 
        any method which is abstract class should be implemented by the concrete class.

export class Manager extends Employee
// This is the child class
     Dowork()
     {
         console.log("Managing People");
     }
}
export class Lead extends Employee
// This is the extended class
     Dowork()
     {
         console.log("Helps people Technically");
     }
}
Explanation: In above example, We have created Manager and Lead
concrete classes or specialization classes. And each and every classes
has its own work. Suppose by mistake we have not defined concrete classes work,
then it will show error.
  • Concrete/specialization class means child class.
      INTERFACE:
  • Interface has such type of properties which can not provide any type of implementation but we can define its properties and function only.
  • Impact Analysis means whenever we change only one places then you will have to change many places.
  • SYNTAX:    
           export interface IEmployee
{
     work();

}
In above example, we have defined an interface IEmployeeWe have implemented IEmployee interface in Employee class by using 'implements' keyword after class name.If we try to differ the data types of properties or signature of method from IEmployee interface in Employee class, then TypeScript compiler will throw an error.

      NEED OF INTERFACES AND ITS DIFFERENCES                                 

INTERFACE

ABSTRACT

1.      In Interface, all members are abstract.

·        But in this case its some members are abstract and some are fully implemented.

2.      Interface supports multiple inheritances.

·        But abstract class does not support multiple inheritances.

3.      Interface is a CONTRACT, LEGAL<, ENFORCEMENT -> impact analysis.

·        Abstract class is a half define parent class

4.      TypeScript Interface has zero JavaScript code.

·        But abstract class compile to JavaScript function.


      Hey Guys if you want more details to this concept (Click here)


  • So friend If you have any mistakes in any articles you can send me on my email:1809raushan@gmail.com.

                                                                                Thank You...

























     

ليست هناك تعليقات:

إرسال تعليق