What is different between Abstract and Interface with example in Angular

 

In Angular (and TypeScript in general), both abstract classes and interfaces are used to define contracts for classes, but they have different use cases and characteristics. Here's a comparison with examples:


Abstract Classes

Purpose: Abstract classes are used to define a base class with common functionality that can be shared among other classes. They can contain method implementations, properties, and constructors.

Inheritance: A class can only extend one abstract class, making it less flexible in terms of inheritance compared to interfaces.

Methods: Abstract classes can have both abstract methods (without implementation) and concrete methods (with implementation).

Example:


Typescript code :

abstract class Shape {

  constructor(public color: string) {}


  abstract getArea(): number; // Abstract method, must be implemented by derived class


  describe(): string {

    return `A ${this.color} shape.`;

  }

}


class Rectangle extends Shape {

  constructor(color: string, public width: number, public height: number) {

    super(color);

  }


  getArea(): number {

    return this.width * this.height;

  }

}

In this example:


Shape is an abstract class with an abstract method getArea and a concrete method describe.

Rectangle extends Shape and implements the getArea method.

Interfaces

Purpose: Interfaces are used to define a contract for classes to follow. They only describe the shape of an object and do not provide any implementation.

Inheritance: A class can implement multiple interfaces, which provides more flexibility compared to abstract classes.

Methods: Interfaces cannot have method implementations; they only declare the method signatures.

Example:


Typescript  code :

interface Shape {

  color: string;

  getArea(): number;

  describe(): string;

}


class Circle implements Shape {

  constructor(public color: string, public radius: number) {}


  getArea(): number {

    return Math.PI * this.radius * this.radius;

  }


  describe(): string {

    return `A ${this.color} circle.`;

  }

}

In this example:


Shape is an interface defining the shape of an object with properties and methods.

Circle implements the Shape interface and provides concrete implementations for getArea and describe.

Summary

Abstract Classes: Useful for sharing common functionality and when you want to provide a base class with some methods already implemented. You can have both implemented and unimplemented methods.

Interfaces: Useful for defining a contract that multiple classes can adhere to without enforcing a specific inheritance structure. They only specify method and property signatures without any implementation.

Choosing between an abstract class and an interface depends on your design needs. If you need to share code and provide a base implementation, use an abstract class. If you need to define a contract that multiple unrelated classes can follow, use an interface.

No comments:

Post a Comment

SQL Commands - essentials

  SELECT # Retrieve data from the database FROM # Specify the table to select data from WHERE # Filter rows based on a condition AS # Rename...

Best for you