please read the detailed SOLID Principles explanation here:
In this TypeScript code, I’ve added explicit type annotations for method parameters and return types, and I’ve also marked abstract methods with the abstract keyword. Additionally, I’ve implemented the necessary interfaces (FlyingBirds and RunningBirds) in the Sparrow and Ostrich classes, respectively, using the implements keyword.
abstract class Bird {
abstract get_legs(): string;
abstract get_wings(): string;
}
abstract class FlyingBirds {
abstract fly(): string;
}
abstract class RunningBirds {
abstract run(): string;
}
abstract class SwimmingBirds {
abstract swim(): string;
}
class CommonBirdFeatures {
get_legs() {
return "This bird has 2 legs";
}
get_wings() {
return "This bird has two wings";
}
}
class Sparrow extends CommonBirdFeatures implements FlyingBirds {
fly() {
return "This bird can fly";
}
make_bird_fly(bird: FlyingBirds): string {
return bird.fly();
}
}
class Ostrich extends CommonBirdFeatures implements RunningBirds {
run() {
return "This bird can run";
}
}
const robin = new Sparrow();
console.log(robin.get_legs());
console.log(robin.make_bird_fly(robin));
-
abstract class Bird { ... }: This defines an abstract class namedBird. Abstract classes in TypeScript cannot be instantiated directly; they serve as blueprints for other classes to inherit from. They can contain abstract methods (methods without a body) that must be implemented by any derived (sub)classes. -
abstract get_legs(): string;: This line declares an abstract method namedget_legswithin theBirdclass. Abstract methods do not have a method body (no implementation details). They only define a method signature (name, parameters, and return type). Any concrete (non-abstract) subclass inheriting fromBirdmust provide an implementation for this method. -
abstract get_wings(): string;: This line declares another abstract method namedget_wingswithin theBirdclass. Likeget_legs, this method also lacks an implementation in the abstract base class and must be implemented by concrete subclasses.
In summary, the Bird class acts as a template for other classes to extend from. Subclasses derived from Bird must implement the get_legs and get_wings methods, providing their specific implementations. This enforces a certain structure and behavior for bird subclasses while allowing for variation in how different types of birds define their legs and wings.