Golang Program to Create Abstract Class


In this article, we are going to learn about how to create an Abstract class using Golang Programming

Abstract Class − A restricted class is called an abstract class which cannot be used to create an object from it to access an abstract class, it must be inherited from another class.

The Go interface lacks fields and forbids the definition of methods inside of it. Any type must implement every interface method in order to belong to that interface type. There are situations in which having a method's default implementation and default fields in GO is helpful. Let's first grasp the prerequisites for an abstract class before learning how to do it −

  • There should be default fields in abstract classes.

  • Default method should be present in abstract classes.

  • Creating a straight instance of the abstract class shouldn't be possible.

Example

An interface (abstract interface) and struct will be combined (abstract concrete type). Together, they can offer an abstract class's features. See the program below −

package main

import "fmt"
// fmt package allows us to print anything on the screen

// Define a new data type "Triangle" and define base and height properties in it
type Triangle struct {
   base, height float32
}

// Define a new data type "Square" and assign length as a property to it.
type Square struct {
   length float32
}

// Define a new data type "Rectangle" and assign length and width as properties to it
type Rectangle struct {
   length, width float32
}

// Define a new data type "Circle" and assign radius as a property to it
type Circle struct {
   radius float32
}

// defining a method named area on the struct type Triangle
func (t Triangle) Area() float32 {
   // finding the area of the triangle
   return 0.5 * t.base * t.height
}

// defining a method named area on the struct type Square
func (l Square) Area() float32 {
   // finding the area of the square
   return l.length * l.length
}

// defining a method named area on the struct type Rectangle
func (r Rectangle) Area() float32 {
   // finding the area of the Rectangle
   return r.length * r.width
}

// defining a method named area on the struct type Circle
func (c Circle) Area() float32 {
   // finding the area of the circle
   return 3.14 * (c.radius * c.radius)
}

// Define an interface that contains the abstract methods
type Area interface {
   Area() float32
}

func main() {
   // Assigning the values of length, width and height to the properties defined above
   t := Triangle{base: 15, height: 25}
   s := Square{length: 5}
   r := Rectangle{length: 5, width: 10}
   c := Circle{radius: 5}

   // Define a variable of type interface
   var a Area

   // Assign to the interface a variable of type "Triangle"
   a = t
   // printing the area of triangle
   fmt.Println("Area of Triangle", a.Area())

   // Assign to the interface a variable of type "Square"
   a = s
   // printing the area of the square
   fmt.Println("Area of Square", a.Area())

   // Assign to the interface a variable of type "Rectangle"
   a = r
   // printing the area of the rectangle
   fmt.Println("Area of Rectangle", a.Area())

   // Assign to the interface a variable of type "Circle"
   a = c
   // printing the area of the circle
   fmt.Println("Area of Circle", a.Area())
}

Output

Area of Triangle 187.5
Area of Square 25
Area of Rectangle 50
Area of Circle 78.5

Description

  • First, we have imported the fmt package.

  • Then, we defined some abstract classes under the name Triangle, Circle, Square and Rectangle and defined properties in them.

  • Next, we have defined some methods corresponding to these abstract classes.

  • Then, we created an Area interface containing an abstract method.

  • Calling the function main().

  • Assigning the length, width and height to the figures respectively to find their area.

  • Defining an interface variable a.

  • Assigning the triangle, square, rectangle and circle to the abstract variable and printing their areas on the screen.

Updated on: 22-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements