Created
November 20, 2019 10:07
-
-
Save Mayankgupta688/107578d1f8f6d2f137ecbde85380df15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Employee interface { | |
GetDetails() string | |
GetEmployeeSalary() int | |
} | |
// Creating a new type "Manager" containing all functions required by "Employee" Interface | |
type Manager struct { | |
Name string | |
Age int | |
Designation string | |
Salary int | |
} | |
func (mgr Manager) GetDetails() string { | |
return mgr.Name + " " + mgr.Age; | |
} | |
func (mgr Manager) GetEmployeeSalary int { | |
return mgr.Salary | |
} | |
// Creating a new type "Lead" containing all functions required by "Employee" Interface | |
type Lead struct { | |
Name string | |
Age int | |
TeamSize string | |
Salary int | |
} | |
func (ld Lead) GetDetails() string { | |
return ld.Name + " " + ld.Age; | |
} | |
func (ld Lead) GetEmployeeSalary int { | |
return ld.Salary | |
} | |
// Creating new Object of Type Manager | |
newLead := Lead{Name: "Mayank", Age: 30, TeamSize: "30" Salary: 10} | |
// New variable of type "Employee" created | |
var employeeInterface Employee | |
//Manager Object assigned to Interface type since Interface Contract is satisfied | |
employeeInterface = newManager | |
// Invoke Functions that belong to Interface "Employee" | |
employeeInterface.GetDetails() | |
//Same Interface can store value of Lead Object | |
employeeInterface = newLead | |
// Invoke Functions of Lead Object that is assigned to Interface "Employee" | |
employeeInterface.GetDetails() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment