Module 3, Object-Orientation in Go, Topic 1.1, Classes and Encapsulation. So, classes are part of object-oriented programming paradigm. And this class, this whole course is for intermediate level people, right, so I'm assuming that people already have exposure to programming. And so it is most likely that you already understand what object oriented programming is, although I will sort of redefine it right now, make sure we have a common understanding. And there's always this question, does Golang support object oriented programming? I'll say yes it does. It doesn't have classes exactly, but it has something equivalent, it allows most of the same features. So just to say what a class is though to start, what a traditional class is, that you would've seen in many other languages. A class is basically a collection of data fields and functions that share a well defined responsibility. So it's data fields and functions put together, usually called methods. You call them methods when they're in a class. So data and methods put together. [COUGH] So as an example, say we want a point class, right, that represent some point in 2D space. So, I'll use a geometry program I'm making, okay? So, the data might be the x coordinate and the y coordinate of the point, that's data that you would associate with a point. The functions, there might be a variety of functions, but maybe I've got distance to origin, quadrants, returns the quadrant that the point's in, AddXO set, AddYO set, set X, set Y, there are a lot of functions that I could put in there. But the point is though, that the data and the functions are all related to the same concept, okay? There is a two-dimensional point and these functions are all things that you do to two-dimensional points. And the data is all the data that's associated with two-dimensional points. So, that's what a class is. Now remember, a class is actually a template. So the class contains data fields but not data. What that means is, when you make this Point class, I'm giving a template on how a point should be created. I'm saying, here's the data that a point should have and here's the function that should operate in that data, but I am not actually providing the data. So a Point class is really a template for a point. An actual point has to have data, has to have an x coordinate and a y coordinate associated with it. So an object is an instantiation of the class, an instance of a class. So it contains actual data. So, as an example, say in my geometry program that I'm trying to make, I have a triangle and this triangle has three points on it, three coordinates, (0,0), (6,0), (5,5). So there are three point objects that I want to create, and I call these objects now. These objects, they're made based on the Point class template so they have x coordinate and y coordinate just like the template says, but they have actual values for those. So the (0,0) point will have 0 and 0 for the x and (5,5) point has 5 and 5 and so on. So for one class you can have many, many objects of that class that instantiate that class. They have actual data in them that fill in the data fields. So, just want to make clear the difference between a class and an object. Encapsulation is another concept that's usually associated with object oriented programming. Actually, you know what, I would say it's associated with the use of abstraction in general, but right now, we're talking about it in the context of object oriented programming. And the idea behind encapsulation is that you might want to hide data from the programmer, okay? So when I say the programmer, I mean the programmer who is using your class. So, the person who's defining the class, you can't hide anything from that person. But if there's a programmer who's using your class, you might want to hide some data, conceal something. So you might want to make the data, allow the data only to be accessed using the methods that are part of the class. So rather than allowing the programmer to just go straight in and modify, say it's a point, just go straight in and modify the x and the y values as a point, we might instead say, look, if you want to modify it, you have to use this method to modify it, the method that's provided in the class. And why would you do this? Maybe we don't trust the programmer to keep the data consistent, that's the main thing. Not that we don't trust the programmer, but the programmer has a lot of things on his or mind, might make a mistake, right? So we want to relieve the programmer that burden of dealing with the internal consistency of the data. So we just say look, programmer, if you use these methods to modify the internal data then we, the people who made the class, guarantee that the data will stay consistent, okay? So you, as a programmer, you don't have to think about that, just use our methods. So that's encapsulation. Where you say look, the internal data can't be accessed directly from the outside, or at least part of it can't be accessed directly from the outside. You basically put up a wall, a hard abstraction barrier, which are the methods that you use to access the data. So as an example, let's say I've got a point and a function, and I want to do this point is to double its distance from the origin, right? So I want to double its x and double its y so it's twice as far along the line between the origin and the point, you want to move that point, you want to scale it, to double it, right, double its distance. So you gotta double the x and double the y. So option one is to make a method to encapsulate, right? So you make a method called double distance and it does exactly that function. That's the safe way. Another way is to say look, I don't want to make that method, I'll just let the programmer directly access the x and y, and the programmer can double the x and y when they want to. The problem with that is what if the programmer makes a little mistake, and doubles the X but forgets to double the Y or doubles the X and triples the Y or whatever? Whatever the mistake, if a mistake like that happens then the X and the Y values inside the object are now inconsistent with each other. And so a mistake was allowed to be made, where if you force them to use this Double Dist function that you made and you know you debugged it correctly, then they can't make such a mistake. Thank you.