
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Show Overriding of Methods in Classes
When a method is overridden in Go, a new method with the same name and receiver type as an existing method is created and used in place of the existing one. As a result, Golang may provide polymorphism, allowing different implementations of a same method to be used depending on the type of receiver. Let' see the execution in examples.
Method 1: Using shape struct
Here, this shape struct will contain an area field and an area method which will return the value of area field. Rectangle and square both inherit the area () method. The output will be their area printed on the console.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program
Step 2 ? First, we create a struct Shape with a field called area of type float64 and a method called Area() that returns the field's value.
Step 3 ? The Shape struct is then embedded in a new structure called Rectangle, which also has two float64-type properties called width and height.
Step 4 ? Then, we create a function for the Rectangle struct called CalculateArea() that computes the area using the width and height variables and assigns the result to the area field that was inherited from the Shape struct.
Step 5 ? The Shape struct is also embedded in the Square struct, which similarly has a field side of type float64.
Step 6 ? Then, using the side field to compute the area and assigning the result to the area field inherited from the Shape struct, we define the method CalculateArea() for the Square struct.
Step 7 ? To compute the areas of the forms, we construct pointers to instances of the Rectangle and Square structs in the main function and call their respective CalculateArea() functions.
Step 8 ? We invoke the Area() function on the Rectangle and Square pointers to obtain the areas of the forms after computing their areas.
Step 9 ? The area of rectangle and the area of square is printed on the console using fmt.Println() function where ln means new line.
Example
In this example we will learn how to override methods in class using shape struct.
package main import ( "fmt" ) type Shape struct { area float64 //create area field in the struct } func (sq *Shape) Area() float64 { return sq.area //return area of square } type Rectangle struct { Shape width float64 //width of rectangle height float64 //height of rectangle } func (rect *Rectangle) CalculateArea() { rect.area = rect.width * rect.height //area of rectangle } type Square struct { Shape side float64 //side of square } func (sq *Square) CalculateArea() { sq.area = sq.side * sq.side //area of square } func main() { rect := &Rectangle{width: 16, height: 6} //set the width and height of square rect.CalculateArea() fmt.Println("Area of rectangle: ", rect.Area()) //print area of rectangle sq := &Square{side: 8} //set side of square sq.CalculateArea() fmt.Println("Area of square: ", sq.Area()) //print area of square. }
Output
Area of rectangle: 96 Area of square: 64
Method 2: Using shape interface
Here, the shapes rectangle and square implement the shape interface by implementing the area method. In the end, the output will be the area of square and rectangle.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program
Step 2 ? In the beginning, we create an interface called Shape with a function called Area() that returns a float64 value.
Step 3 ? We then define a structure called Rectangle, which has two float64-type attributes called width and height.
Step 4 ? The Area() method for the Rectangle struct is then implemented by creating a function with the same signature as the Shape interface and computing the area using the width and height parameters.
Step 5 ? In a similar manner, we create a structure called Square with a field of type float64.
Step 6 ? Then, using the side field to determine the area this time, we implement the Area() method for the Square struct.
Step 7 ? The Rectangle struct's Area() method is called in the main function, and it returns the area of the rectangle.
Step 8 ? Additionally, we construct an instance of the Square struct and use its Area() method to obtain the square's area.
Step 9 ? The area of both the shapes is printed on the console using fmt.Println() function where ln means new line.
Example
In this example we will learn how to override methods in class using shape interface.
package main import ( "fmt" ) type Shape interface { Area() float64 //create area method } type Rectangle struct { width float64 //width of rectangle height float64 //height of rectangle } func (rect Rectangle) Area() float64 { return rect.width * rect.height //area of rectangle } type Square struct { side float64 //side of square } func (sq Square) Area() float64 { return sq.side * sq.side //area of square } func main() { rect := Rectangle{width: 16, height: 6} //set the width and height of rectangle fmt.Println("Area of rectangle: ", rect.Area()) //print area of rectangle sq := Square{side: 8} //set the sides of square fmt.Println("Area of square: ", sq.Area()) //print area of square }
Output
Area of rectangle: 96 Area of square: 64
Conclusion
We executed the program of showing how to override methods in class using two examples. In the first example we used shape struct and in the second example we used shape interface.