DEV Community

vinay
vinay

Posted on

Polymorphism in Golang

#go

1.

package main

import "fmt"

//creating class
type PersonClass struct {
    name  string
    age   string
    color string
}

//person one have class properties and function
type Person1 interface {
    Walking() string
    Runnig() string
}

//person two have class properties and Person1 function and extra function
type Person2 interface {
    Person1
    Sleping() string
    Dringking() string
}

//
//person tree have class properties and Person1 function and Person2 ,extra function
type Person3 interface {
    Person2
    Learing() string
}

func (p PersonClass) Walking() string {
    return "walking :" + p.name
}
func (p PersonClass) Runnig() string {
    return "runnig :" + p.name
}

func (p PersonClass) Sleping() string {
    return "Im sleping :" + p.name
}
func (p PersonClass) Dringking() string {
    return "Im drinking :" + p.name
}

func (p PersonClass) Learing() string {
    return "Learing :" + p.name
}

func main() {

    w := []Person1{
        PersonClass{"mr.Boom", "24", "Oraange"},
    }

    x := []Person2{
        PersonClass{"mr.Hello", "22", "brown"},
    }
    p := []Person3{
        PersonClass{"mr.kk", "21", "red"},
    }
    fmt.Println("Person 1")
    for _, v := range w {
        fmt.Println(v.Runnig(), v.Walking())
    }
    fmt.Println("Person 2")

    for _, v := range x {
        fmt.Println(v.Dringking(), v.Runnig(), v.Sleping(), v.Walking())
    }
    fmt.Println("Person 3")
    for _, v := range p {
        fmt.Println(v.Walking(), v.Runnig(), v.Dringking(), v.Sleping(), v.Learing())
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
marcello_h profile image
Marcelloh

ok, I can advice you to use a spellchecker, because I think you're not a native English speaker. (and it distracts a lot from what you are showing)
Btw: I would tell a bit of a story about what you are showing.

Collapse
 
vinaygo profile image
vinay

now on words I will Chek and I will post

Collapse
 
vinaygo profile image
vinay

yes I write code only I didn't Chek spelling