"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Doesn\'t Go Support Traditional Inheritance?

Why Doesn\'t Go Support Traditional Inheritance?

Published on 2024-11-08
Browse:608

 Why Doesn\'t Go Support Traditional Inheritance?

Inheritance in Go

Why doesn't Go support traditional type inheritance?

Traditional type inheritance, where a subclass inherits the definitions of one or more parent classes, is not a feature of the Go programming language.

Creators' Rationale

In the Go FAQ, the language creators explain that object-oriented programming languages often emphasize defining relationships between types, which can be automatically inferred in Go. Instead of explicitly specifying type relationships, Go types automatically satisfy any interface that specifies a subset of their methods.

Benefits of Go's Approach

This approach provides several advantages:

  • Simplified Bookkeeping: There is no need to explicitly declare type relationships.
  • Interface Flexibility: Types can satisfy multiple interfaces at once, avoiding the complexities of traditional multiple inheritance.
  • Lightweight Interfaces: Interfaces can have one or zero methods, allowing for the expression of useful concepts without the need for additional type annotations.
  • Flexibility for Changes: Interfaces can be added after the fact, providing flexibility for new ideas or testing without modifying original types.
  • Absence of Type Hierarchy: The lack of explicit relationships between types eliminates the need to manage or discuss type hierarchies.

Alternative Principle

Go promotes the principle of composition over inheritance. This involves creating new types by composing existing types, rather than using inheritance to create a new type with all the features of its parent types.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3