Go Inheritance and Polymorphism

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

Go does not have formal inheritance at language level. Inheritance can be implemented via a combination of struct field embedding and interfaces. The Inheritance section of this article explains how that is done. Once an implicit inheritance structure is put in place, polymorphism is also available.

Inheritance

For more general information on inheritance see:

Inheritance

Overriding

Polymorphism

Polymorphism is available in Go, but it is implemented differently than in other object-oriented languages.

In other OOP languages, there is a formal extends language keyword or other formal syntax that expresses the inheritance relationship between classes. In Java, the inheritance is formally declared with the extends keyword:

class SomeSubclass extends SomeSuperclass {
  ...
}

In Python, we formally declare that a class extends other class with this syntax:

class SomeSubclass(SomeSuperclass):
  ...

o, in that it does not imply formal inheritance at language level, which is not available in Go. Polymorphism in Go is implemented using interfaces.

For a generic discussion on polymorphism, see:

Object-Oriented Programming | Polymorphism

Overriding

TO DEPLETE

Read and merge into document: