Go Inheritance and Polymorphism: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 5: Line 5:


=Inheritance=
=Inheritance=
In its most generic form, inheritance in programming languages is the capability of declaring class hierarchies. A hierarchy includes generic classes, called superclasses, that are inherited by more specific and specialized classes, called subclasses. The superclasses declare attributes (state) and behavior that are shared with all their descendants. This is a great capability when it comes to code reusability: that state and behavior is declared only once, in superclass, and it does not have to be repeated in subclasses.
The subclasses also have the capability to declare state and behavior that is particular to them only, and differentiate them from siblings in the hierarchy.


For more general information on inheritance see: {{Internal|Object-Oriented_Programming#Inheritance|Inheritance}}
For more general information on inheritance see: {{Internal|Object-Oriented_Programming#Inheritance|Inheritance}}
All this can be implemented in Go, but without formal support from the language.


=Overriding=
=Overriding=

Revision as of 22:04, 4 August 2024

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

In its most generic form, inheritance in programming languages is the capability of declaring class hierarchies. A hierarchy includes generic classes, called superclasses, that are inherited by more specific and specialized classes, called subclasses. The superclasses declare attributes (state) and behavior that are shared with all their descendants. This is a great capability when it comes to code reusability: that state and behavior is declared only once, in superclass, and it does not have to be repeated in subclasses.

The subclasses also have the capability to declare state and behavior that is particular to them only, and differentiate them from siblings in the hierarchy.

For more general information on inheritance see:

Inheritance

All this can be implemented in Go, but without formal support from the language.

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: