Inheritance in Golang
Inheritance in Golang is a way of creating a child class that is a modified version of an existing or parent class. The newly created class takes the properties of the existing parent class. The subclass or child class can also modify or add some new properties in the parent class.
Syntax for Inheriting in Golang
To inherit from a parent class in Golang, we use the struct keyword and embed the parent class inside the child class. Here’s an example:
property1 string
property2 int
}
type Child struct {
Parent
property3 float32
}
The above syntax has a Child struct that embeds the Parent struct, which means it inherits all its properties and methods. The Child struct also has a new property called property3.
Why Golang Doesn’t Support Inheritance
In traditional object-oriented programming, inheritance is a fundamental concept that involves inheriting properties from a parent class to a child class. However, since Golang does not support classes, inheritance is implemented using struct embedding.
In Golang, structs cannot be extended directly, so we use composition to construct new objects using the struct. Therefore, it is accurate to say that Golang does not provide native support for inheritance, but rather supports composition as an alternative.
How to Achieve Inheritance in Golang Using Struct Embedding
In Golang, inheritance is achieved through a mechanism called struct embedding. This allows us to embed one struct inside another, which results in a relation like a parent and child class. The child struct can then access the fields and methods of the parent struct, as if they were part of its own definition.
Here’s an example of how to achieve inheritance in Golang using struct embedding:
name string
age int
}
type Employee struct {
Person // Embedding the Person struct inside the Employee struct
empID int
salary float64
}
In the above code, the Person struct is embedded by Employee struct using Person field name without a type, which implies that it is embedding the struct itself. This creates a parent-child relationship between the two structs, where the Employee struct is the child, and the Person struct is the parent.
Now, any fields or methods defined in the Person struct can be accessed from the Employee struct. For example, we can access the name field of the Person struct from the Employee struct using the dot notation:
fmt.Println(e.name)
Similarly, we can define methods in the Person struct that can be accessed from the Employee struct:
fmt.Printf("Hi, my name is %s and I am %d years old.\n", p.name, p.age)
}
func main() {
e := Employee{Person{name: "Kash", age: 24}, 20, 1000}
e.introduce()
}
Example 1: Golang Program to Show Inheritance in Struct Embedding
import "fmt"
type Person struct {
name string
age int
}
type Employee struct {
Person // Embedding the Person struct inside the Employee struct
empID int
salary float64
}
func (p *Person) introduce() {
fmt.Printf("Hi, my name is %s and I am %d years old.\n", p.name, p.age)
}
func (e *Employee) introduce() {
fmt.Printf("Hi, my name is %s and I am %d years old. My employee ID is %d and my salary is $%.2f.\n", e.name, e.age, e.empID, e.salary)
}
func main() {
e := Employee{Person{name: "Kash", age: 24}, 20, 1000}
fmt.Println(e.name)
e.introduce()
}
Here, we have defined a Person struct and an Employee struct that embeds the Person struct. We then define the introduce() method on the Person struct, which we can access from the Employee struct. In main(), we created a new Employee instance and demonstrated how we can access the fields and methods of the embedded Person struct.
Example 2: Golang Program to Show Multiple Inheritances to a Struct
import "fmt"
type Person struct {
name string
age int
}
type Student struct {
Person // Embedding the Person struct inside the Student struct
rollNo int
}
type Teacher struct {
Person // Embedding the Person struct inside the Teacher struct
empID int
}
func (p *Person) introduce() {
fmt.Printf("Hi, my name is %s and I am %d years old.\n", p.name, p.age)
}
func (s *Student) study() {
fmt.Printf("%s is studying.\n", s.name)
}
func (t *Teacher) teach() {
fmt.Printf("%s is teaching.\n", t.name)
}
func main() {
s := Student{Person{name: "Kash", age: 24}, 1001}
t := Teacher{Person{name: "Sam", age: 30}, 2001}
s.introduce()
s.study()
t.introduce()
t.teach()
}
In the example above, we define a Person struct and two other structs, Student and Teacher, that embed the Person struct. We also define introduce(), study(), and teach() methods on the Person, Student, and Teacher structs, respectively.
In main(), we created new instances of both Student and Teacher and demonstrated how we can access the fields and methods of the embedded Person struct, as well as the methods specific to each struct.
Conclusion
Inheritance is an essential concept in object-oriented programming, and Golang provides support for single inheritance through struct embedding. For multiple Inheritance in Golang, the composition method is used. This article covers how we can achieve the inheritance using composition method in Golang. For further details on inheritance in Golang, read the article.