Inheritance in GoLang
Inheritance means inheriting the properties of the superclass into the base class and is one of the most important concepts in Object-Oriented Programming. Since Golang does not support classes, so inheritance takes place through struct embedding. We cannot directly extend structs but rather use a concept called composition where the struct is used to form other objects. So, you can say there is No Inheritance Concept in Golang.
In composition, base structs can be embedded into a child struct and the methods of the base struct can be directly called on the child struct as shown in the following example.
Universe is: MCU Universe is: DC
Multiple inheritances take place when the child struct is able to access multiple properties, fields, and methods of more than one base struct. Here the child struct embeds all the base structs as shown through the following code:
In base struct 1. In base struct 2.
Please Login to comment...