Introduction to Encapsulation and Polymorphism in Golang

Encapsulation and Polymorphism in Golang



The encapsulation provides a lot of different support for encapsulation and keeping private data. But you want to be able to have controlled access to the data. So typically, even if you have private data in some package, you probably don’t want to hide it completely. but you want to have controlled access to it. It means is you want people to be able to use that data but only in the way that you define, using your methods or functions.

you can define a set of functions, public functions, that allow another package, an external package, to access the hidden data using encapsulation.

here we have an example,

 package data
 var x int = 1
 func PrintX()

The main package will be able to access this PrintX() method even though it can’t directly access the x. Now, you can access the main method, the main function can access the x variable only through this PrintX() function.

 package main
 import "data"
 func main()

So if you look in the main code, you import the data and then in my main. then you can call data.PrintX and then you can see the value of x. Even though you couldn’t access x from my main, you can access it through these public functions using encapsulation. So basically, we’re going to control access to data that we want to hide.


Polymorphism is a property very associated with object-oriented programming. It’s the ability for an object to have different forms depending on the context. actually, what it means is you can have a function or method with one name, area. And it does one thing for one object and another thing for another type of object.

For example, let’s take area() function, If you want to compute the area of a rectangle, that’s base times height. here we’re going to compute the area of a triangle as one-half base times height.

  area = base * height
  area = 0.5 * base * height

A function name is going to do two different things depending on the context. If you’re doing it about a rectangle or on a triangle, so that’s what polymorphism is. So you would say the area is polymorphic because it can do two different things depending on the context.

the polymorphism is a way of establishing an extraction. These things are the same at a high level of extraction. So one thing that is usually used in object-oriented languages to support polymorphism is an inheritance, and Golang does not have inheritance.

Inheritance is where you get a series of classes, and they have this class, subclass, superclass relationship, or sometimes you call it parent and child class, parent class, child class. So the superclass is the top-level class, and the subclass extends from the superclass. And the subclass inherits the methods and data of the superclass.


We a speaker superclass, and a speaker is supposed to represent everything that can speak. Now, underneath the speaker, a subclass of that might be the cat and it might be a dog. because cats can speak. They can make a sound. Dogs can make a sound. And cat and dog will both inherit the properties of the superclass.

Speaker has a speak() method.

Cat and Dog have the same speak() method.

The cat and dog are different forms of the speaker. they are different forms of each other, this is where polymorphism concepts come into play. And remember that Golang doesn’t have the inheritance. Actually, inheritance is one thing that you use in a regular object-oriented language to support polymorphism, but you also, on top of that, you’re going to need another property, overriding.


The ability to override a method. So a method is overridden, a subclass overrides a method when it redefines a method that it inherits from the superclass.

the subclass cat and subclass dog. And the speaker superclass has this speak() method and cat and dog inherit the speak method. But, without overriding the speak method, the cat speak method and the dog speak method to do exactly what the superclass, what the speaker’s speak method does. They print out the sound.

So what happens is, that’s called overriding. The cat subclass will define again the speak method to print out what it wants to sound. And then the dog subclass will also define again to print out what it wants. So now the speaker superclass, it has its speak method, and cat also the speak method, the dog also the speak method. But the cat speaks and the dog speak do two different things. the cat and dog classes have overridden the definition of the speak method with their own new definition of the speak method.

Actually, you can say speak is polymorphic. Because it speak, it has two different implementations for each class. To speak in the context of the cat, it’ll print cat sound. Context of the dog, it’ll print dog sound. But the idea is to support polymorphism which you normally see in an object-oriented language,

we have seen the inheritance, and then we also see the ability to override a method. the both sub-classes inherit to the speak method, but then they can override it and define it the way they want. this function signature is the same. the speak method, it has the same name, in the cat and dog class, it has the same arguments with the same return types.

Leave a Reply

Your email address will not be published. Required fields are marked *