Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Go Inheritance

Go Inheritance

In object-oriented programming (OOP), inheritance is a mechanism that allows one class (the subclass) to inherit properties and methods from another class (the superclass). Through inheritance, a subclass can reuse the parent class's code and can extend or modify its behavior without changing the parent class.

Go is not a traditional object-oriented programming language; it has no concepts of classes and inheritance.

Go uses structs and interfaces to achieve similar functionality.


"Inheritance" in Go

Go does not have the concepts of class or inheritance found in traditional object-oriented languages; instead, it achieves similar functionality through composition and interfaces.

1. Composition

Composition is the primary way to achieve code reuse in Go. By embedding one struct into another, the child struct can "inherit" the fields and methods of the parent struct.

Example

package

main

import

"fmt"

// Parent struct

type

Animal

struct

{

    Name

string

}

// Method of the parent struct

func

(

a

Animal

)

Speak

()

{

    fmt

.

Println

(

a

.

Name

,

"says hello!"

)

}

// Child struct

type

Dog

struct

{

    Animal

// Embed the Animal struct

    Breed  

string

}

func

main

()

{

    dog

:=

Dog

{

        Animal

:

Animal

{

Name

:

"Buddy"

},

        Breed

:

 

"Golden Retriever"

,

   

}

    dog

.

Speak

()

// Call the parent struct's method

    fmt

.

Println

(

"Breed:"

,

dog

.

Breed

)

}

Code explanation


2. Interface

Interfaces are the primary way to achieve polymorphism in Go. By defining interfaces, different structs can implement the same methods, achieving polymorphic behavior similar to inheritance.

Example Code

Example

package

main

import

"fmt"

// Define an interface

type

Speaker

interface

{

    Speak

()

}

// Parent struct

type

Animal

struct

{

    Name

string

}

// Implement interface methods

func

(

a

Animal

)

Speak

()

{

    fmt

.

Println

(

a

.

Name

,

"says hello!"

)

}

// Child struct

type

Dog

struct

{

    Animal

    Breed

string

}

func

main

()

{

   

var

speaker Speaker

    dog

:=

Dog

{

        Animal

:

Animal

{

Name

:

"Buddy"

},

        Breed

:

 

"Golden Retriever"

,

   

}

    speaker

=

&

dog

    speaker

.

Speak

()

// Call a method through an interface

}

Code explanation

Differences between Go and classical inheritance

Feature Classical inheritance Go's approach
Code Reuse By extending Through composition (embedding structs)
Polymorphism Through inheritance and method overriding Through interface implementation
Relational "is-a" relationship "has-a" or "implements" relationship
Flexibility Inheritance relationship is fixed Can be composed at runtime

** Complete inheritance simulation:**

Example

package

main

import

"fmt"

// Base class

type

Vehicle

struct

{

    Brand

string

}

func

(

v

Vehicle

)

Start

()

{

    fmt

.

Println

(

v

.

Brand

,

"started"

)

}

// Derived class

type

Car

struct

{

    Vehicle

// Embed Vehicle

    Model  

string

}

// Override the Start method

func

(

c

Car

)

Start

()

{

    fmt

.

Println

(

c

.

Brand

,

c

.

Model

,

"car started"

)

}

func

main

()

{

    v

:=

Vehicle

{

Brand

:

"Toyota"

}

    c

:=

Car

{

        Vehicle

:

Vehicle

{

Brand

:

"Honda"

},

        Model

:

 

"Civic"

,

   

}

   

    v

.

Start

()

// Toyota started

    c

.

Start

()

// Honda Civic car started

    c

.

Vehicle

.

Start

()

// Honda started

}

This design of Go avoids many problems of traditional inheritance, such as the fragile base class problem, while providing greater flexibility.