📜  extends vs implements java - TypeScript (1)

📅  最后修改于: 2023-12-03 15:30:41.740000             🧑  作者: Mango

Extends vs Implements in Java and TypeScript

In Java and TypeScript, extends and implements are two important keywords that allow for class inheritance and interface implementation. While they both serve similar purposes, they have different use cases and implications.

Overview

extends is used to create a subclass that inherits attributes and methods from a parent class. The child class is a more specific version of the parent class, with the ability to add or override methods and attributes as needed.

implements is used to define a class that follows a specific interface. An interface is a set of methods that a class must implement, without providing any implementation details. This allows for multiple classes to share the same interface, while having different implementation logic.

Java Examples
Extends
class Animal {
  private String name;

  public Animal(String name) {
    this.name = name;
  }

  public void speak() {
    System.out.println("The animal speaks.");
  }
}

class Dog extends Animal {
  public Dog(String name) {
    super(name);
  }

  @Override
  public void speak() {
    System.out.println("The dog barks.");
  }
}

// Usage
Animal animal = new Animal("Generic animal");
Dog dog = new Dog("Spot");

animal.speak(); // The animal speaks.
dog.speak(); // The dog barks.

In this example, the Animal class is extended by the Dog class. The Dog class inherits the name attribute and speak() method from the Animal class, but also overrides the speak() method to provide a more specific implementation.

Implements
interface Shape {
  public double area();
  public double perimeter();
}

class Circle implements Shape {
  private double radius;

  public Circle(double radius) {
    this.radius = radius;
  }

  @Override
  public double area() {
    return Math.PI * Math.pow(radius, 2);
  }

  @Override
  public double perimeter() {
    return 2 * Math.PI * radius;
  }
}

// Usage
Shape circle = new Circle(5);

System.out.println("Circle area: " + circle.area()); // Circle area: 78.53981633974483
System.out.println("Circle perimeter: " + circle.perimeter()); // Circle perimeter: 31.41592653589793

In this example, the Shape interface is implemented by the Circle class. The Circle class must implement the area() and perimeter() methods defined in the Shape interface, and can provide its own implementation logic.

TypeScript Examples
Extends
class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  speak() {
    console.log("The animal speaks.");
  }
}

class Dog extends Animal {
  constructor(name: string) {
    super(name);
  }

  speak() {
    console.log("The dog barks.");
  }
}

// Usage
let animal = new Animal("Generic animal");
let dog = new Dog("Spot");

animal.speak(); // The animal speaks.
dog.speak(); // The dog barks.

In this example, the TypeScript syntax is similar to the Java example. The Animal class is extended by the Dog class, with the super keyword used to call the parent constructor.

Implements
interface Shape {
  area(): number;
  perimeter(): number;
}

class Circle implements Shape {
  radius: number;

  constructor(radius: number) {
    this.radius = radius;
  }

  area() {
    return Math.PI * Math.pow(this.radius, 2);
  }

  perimeter() {
    return 2 * Math.PI * this.radius;
  }
}

// Usage
let circle: Shape = new Circle(5);

console.log("Circle area: " + circle.area()); // Circle area: 78.53981633974483
console.log("Circle perimeter: " + circle.perimeter()); // Circle perimeter: 31.41592653589793

In TypeScript, the syntax for implementing an interface is very similar to Java. The Shape interface is implemented by the Circle class, with the required methods defined within the class.

Conclusion

In summary, extends and implements are important concepts to understand in Java and TypeScript. extends is used for class inheritance, while implements is used for interface implementation. By understanding these concepts, you can create more flexible and modular code in your applications.