Go Inheritance and Polymorphism: Difference between revisions
Line 29: | Line 29: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
All sub-types, like a <code>Car</code> or a <code>Plane</code>, share a "speed" attribute that does not needed to be declared when we declared the corresponding types, as long as we embed the base type <code>vehicle</code>: | All sub-types, like a <code>Car</code> or a <code>Plane</code>, share a "speed" attribute that does not needed to be declared when we declared the corresponding types, as long as we embed the base type <code>vehicle</code>. Each sub-type can declare its own specific fields, which are particular to the sub-type and not shared via the base type: | ||
<span id='Car'></span><syntaxhighlight lang='go'> | <span id='Car'></span><syntaxhighlight lang='go'> | ||
package transportation | package transportation | ||
Line 46: | Line 46: | ||
type Plane struct { | type Plane struct { | ||
vehicle | vehicle | ||
Wingspan int # Wingspan in feet, rounded to the closest foot. | |||
} | } | ||
func NewPlane(speed int) *Plane { | func NewPlane(speed int, wingspan int) *Plane { | ||
return &Plane{vehicle{Speed: speed}} | return &Plane{vehicle{Speed: speed}, wingspan} | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 55: | Line 56: | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
c := transportation.NewCar(100) | c := transportation.NewCar(100) | ||
p := transportation.NewPlane( | p := transportation.NewPlane(802, 197) | ||
fmt.Printf("car speed: | fmt.Printf("car speed: %d mph\n", c.Speed) | ||
fmt.Printf("plane speed: %d mph\n", p.Speed) | fmt.Printf("plane speed: %d mph\n", p.Speed) | ||
fmt.Printf("plane wingspan: %d feet\n", p.Wingspan) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang='text'> | <syntaxhighlight lang='text'> | ||
car speed: | car speed: 100 mph | ||
plane speed: | plane speed: 802 mph | ||
plane wingspan: 197 feet | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 22:55, 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. The generic 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 as defined above ca be implemented in Go, but without formal support from the language. State inheritance is implemented using struct embedding. Behavior inheritance is implemented using interfaces.
Example Hierarchy
We use an example hierarchy involving vehicles. The base type, and the specialized types are declare in their own source files, in the same transportation
package:
pkg └── transportation ├── vehicle.go ├── car.go └── plane.go
State Inheritance
We implement state inheritance with struct embedding. We declare a base struct
that contains all the attributes to be shared by inherited types. In this case, in a vehicle hierarchy, the base type is vehicle
. The base type is declared package-private, as it does not need exposure outside the package. It contains fields that are common to all members of the hierarchy, like Speed
, for example. The fields that need to be visible outside the package as fields of the sub-types (see Car
and Plane
below) are declared as package-public:
package transportation
type vehicle struct {
Speed int
}
All sub-types, like a Car
or a Plane
, share a "speed" attribute that does not needed to be declared when we declared the corresponding types, as long as we embed the base type vehicle
. Each sub-type can declare its own specific fields, which are particular to the sub-type and not shared via the base type:
package transportation
type Car struct {
vehicle
}
func NewCar(speed int) *Car {
return &Car{vehicle{Speed: speed}}
}
package transportation
type Plane struct {
vehicle
Wingspan int # Wingspan in feet, rounded to the closest foot.
}
func NewPlane(speed int, wingspan int) *Plane {
return &Plane{vehicle{Speed: speed}, wingspan}
}
Note that Car
and Plane
are exported, presumably we need to use these types outside the package, unlike the base type vehicle
. The base type state becomes available as sub-types state:
c := transportation.NewCar(100)
p := transportation.NewPlane(802, 197)
fmt.Printf("car speed: %d mph\n", c.Speed)
fmt.Printf("plane speed: %d mph\n", p.Speed)
fmt.Printf("plane wingspan: %d feet\n", p.Wingspan)
car speed: 100 mph
plane speed: 802 mph
plane wingspan: 197 feet
Behavior Inheritance
Overriding
Polymorphism
Polymorphism is available in Go, but it is implemented differently than in other object-oriented languages.
For a generic discussion on polymorphism, see:
TO DEPLETE
Read and merge into document: