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.
- Zero value in Golang
- Composition in Golang
- Closures in Golang
- Reflection in Golang
- Interfaces in Golang
- Packages in Golang
- What is Regex in Golang?
- Hello World in Golang
- Methods With Same Name in Golang
- Using WaitGroup in Golang
- Encapsulation in Golang
- Recover in Golang
- Panic in Golang
- Rune in Golang
- How to copy a map to another map in Golang?
- How to use Ellipsis (...) in Golang?
- Methods in Golang
- Templates in GoLang
- Pointers in Golang
- Polymorphism in GoLang
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.