Can you tell me what a Class is?Rob ParkerBlockedUnblockFollowFollowingApr 15….
is the one of the questions I was asked during a phone interview last week.
A class is something I see and use almost every day when programming, but when asked to describe what a class is, I struggled a little.
Which is interesting because you would think that something you see and use everyday would be easy to describe to someone.
I guess I had never really had to think, and then describe in detail what a class.
In my head I understood it and that was that….
very foolish of me.
So what is a Class?In Computer Science jargon a class is described as the following.
In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).
 In many languages, the class name is used as the name for the class (the template itself), the name for the default constructor of the class (a subroutine that creates objects), and as the type of objects generated by instantiating the class; these distinct concepts are easily conflated.
Pulled from Wikipedia, It’s a solid explanation, and I wouldn’t be ashamed to give that in response to a question asking what a Class is.
So what if the next question was, what defines a Class behaviour or structure….
Let’s take a look at a Class in JS.
The very simple Class contains a constructor method to set the variables when the class is instantiated and a getter and setter method.
Getter functions are meant to simply return (get) the value of an object’s private variable to the user without the user directly accessing the private variable.
Setter functions are meant to modify (set) the value of an object’s private variable based on the value passed into the setter function.
This change could involve calculations, or even overwriting the previous value completely.
The behaviour of class or its instances is defined using methods.
Methods are subroutines with the ability to operate on objects or classes.
These operations may alter the state of an object or simply provide ways of accessing it.
Let’s see how it all works.
Firstly we need to instantiate the class to create an instance of the class.
The syntax for class creation very much depends on the type of class and the contents of its constructor method.
In my example I create a variable named “bob” and using the “new” key word I assign it an instance of Phonebook passing through a name and phone number.
Pretty cool right?.So we have a variable that contains an instance of the PhoneBook class we now can access the data stored within the object with the get methods inherited from the class.
It only gets getter…By using dot notation we can access the get contactName and contactNumber getter methods by adding the variable name that defined in the constructor method.
Very similar to the dot notation we used to access the data stored in our object variables we update the in same way but assign a value to the variable.
This is only possible due the setter methods inherited from the class.
Classes are awesome, and so is Object Oriented Programming.
I would recommend checking out FreeCodeCamp for more content and tutorials on Classes and Objects.