What is a class and what is an instance?
In part 1 of this series – what is a class – we very briefly outlined what one is. In today’s post, I will delve a little deeper. This post was inspired by a wonderful StackOverflow answer I came across. I will be adopting it for the most part and making changes where necessary:
What is a class?
- A class is basically a blue print. In the last post, the blue print we used was a cookie cutter, and the instances of that class where the cookies. Let us use another example to reinforce the concept: in this case, let us say that we have a blue print for building a car. In this case, the Car class is basically a blue print for producing cars. This blue print can be utilised to produce many cars.
- Every individual car that is produced using that blue print is said to be an:
- instance of the car class or
- an object of the car class. Or,
- an instance of type car. Or,
- an object of type car.
- Every car you build as an address – a place where it is parked. If you want to tell someone how to get to your car, well then you hand them a piece of paper which tells them the card’s address. This piece of paper is called a reference to the car instance.
Main Code / Client Code:
Car bensCar = new Car();
TestCar(bensCar);
bensCar.CarWash(); // wash the car
public void TestCar(Car fredsCar){
fredsCar = new Car();
Console.WriteLine("Let's now crash and destroy this car");
car.Destroy();
}
public class Car
{
public void CarWash(){}
public void Destroy(){}
}
Let’s go through things step by step:
- Line 1. We create a new car (in the factory). This is what the ‘new Car()’ does. It creates a new car. Then we assign, to the car just created, a “reference” called ‘bensCar’. This reference is a piece of paper which gives the car a unique name and it’s basically our way of referring to that particular car. If ever we want to wash bensCar then we have to say: computer – go and wash bensCar. We do this by writing: bensCar.Wash(). We call the Wash() method on the bensCar object. In other words, bensCar basically tells everyone where the car actually is. Once we know where the car is, then we can do all sorts of things with it.
- Line 2: We want to test benscar. This is how it the concept is expressed in geek language: we are ‘calling’ TestCar and we ‘pass’ bensCar into the TestCar method. This means that bensCar is an input into the TestCar method. Or another way of saying that is to say: TestCar accepts a Car parameter; bensCar was the ‘argument’ in this particular case. All of this might not make sense to you immediately. What is a parameter? What is an argument? What is meant by ‘passing in’? What is a method? It’s all too much! Hang on, hang on! Let’s take it slowly: see at the bottom of this article a step by step explanation of what is going on.
- To put it more simply, we have the address of bensCar written on a piece of paper. We give this piece of paper to the TestCar method. The test car method takes this piece of paper and makes a photo copy of it. The piece of paper still tells everyone where they can find bensCar. Except that this piece of paper, because it is a photo copy is called: fredsCar. Then what happens is that somebody crosses out the address of bensCar written on the photo copy and writes down the address of a newly created car! Now if you want to get to bensCar while you are in the TestCar method – you can’t do that anymore. You’ve crossed out the original address and written your own one in. And now, fredsCar is taken and is basically destroyed. Does this affect bensCar in any way? Not at all. bensCar is still alive and well.
- Line 3: somebody looks at the address of bensCar – he then goes to the car and gives it a good CarWash. FredsCar doesn’t exist here anymore. Why? Because we don’t have an address to FredsCar. That means the garbage collector will make sure it no longer exists.
Summary
- We ask C# to build a car. A car is built. When this happens we get a piece of paper which tells us the address of the car. This address is called bensCar.
- TestCar is called. We give test car a copy of the piece of paper which has the address of bensCar on it. This new piece of paper is called fredsCar. fredsCar points the way to bensCar. Now there are two pieces of paper which tell us where the one car is.
- Within the TestCar method, another car is created. A new car. We take the photocopied piece of paper – which is called fredsCar and we erase the location of bensCar and write down the location of the new car which was just built on that piece of paper. Now fredsCar points to the new car which was just created. We call the destroy method on fredsCar. This means that using the address on that piece of paper, we physically go to fredsCar (which is the new car just created) and we “destroy” it.
- Back in the main method (or ‘client’) we still have the piece of paper which refers to bensCar. This paper of course still points to bensCar. We go to the car and we wash it.
I hope that makes some sort of sense. If not, please let me know which bits are confusing and I will try to rewrite it for you.
Methods, parameters, arguments and returned values
Notice the above imaginary machine. It is a method. All methods work like this. You put something into the method and at the end of the day you get something out. You can even have a method which does not return anything back to you. These methods return a ‘void’ – that means they don’t return anything.
Let’s break this down step by step:
- The machine is a method.
- The method accepts 2 numbers. The message accepts two “parameters” which must be of type number.
- The arguments in this case are the numbers: 4 and 2.
- The out put or returned value is: 6.
- We “pass in” the arguments: 4 and 2 to the Adding method. And it returns 6.
- The unique combination of the method name (“Adding”) and the types of the parameters which are passable into the method (i.e. the two numbers) are what is called a “method signature”. You can have only one unique method signature combination. In other words, you cannot have another adding machine which also accepts two numbers and returns one number. The method signature needs to be different: perhaps it accepts three numbers. If the Adding method accepts three numbers, then you can still have two methods that are called the same because their method signatures are different: one accepts only two arguments while the other accepts three.