@coolgirl123
@aydanshirin
Hey guys, I’m very new to this so I may be wrong but my understanding of classes and instances, methods and constructors is this;
Class: Think of the class as a box. Within a program your coding you might have 10 boxes. Each box is used to define and do different things within the program. (For example, if your writing a program for a university you might have a class for students, a class for teachers, a class for courses, a class for alumni, etc.) So a class is really a set of data and instructions within your code that relates to one thing. (Students, teachers, etc)
Instance: So if we have a class for students we’ll need to store data in this class about each student at the university. Lets say the data we have for each student consists of their name, age and exam results. Within the CLASS we’ll need to our data as variables. (string for name, int for age and double for exam results) Within the METHOD we then create instances. Think of the instance as each individual student at the university. The variables are student name , age and exam results whilst the instance is the individual student. (i. .eTom Smith, 21, 71.23) If you had 100 students at the university you’d have 3 variables (student name, age, exam results) and 100 instances. (1 for each student)
Method & Constructor: Think of the method the same way as you would a cooking recipe. The method tells you what to do with the ingredients. The ingredients are our instances. Now when we read a recipe we as humans know what the ingredients are and what the amounts mean. (e.g. 2 eggs, 300g flour, 200ml milk - we know what all of these things are so all we need are the different ingredients and we can follow the method to make our meal) Computers don’t know what the ingredients (instances) are! So even if we enter our different instances (i.e. data) into our code and give perfect instructions of what to do with the instances it won’t know what egg, flour or milk means. This is why we have a constructor. The constructor is created within the class to tell the program what an egg is, what milk is, etc.
In the previous example of the university within our class of students we have 3 variables (student name, age, exam results) and 100 instances (one for each student) Just like the cooking recipe the program doesn’t know what a student name, age or exam result is. So we make a constructor to tell the program the data type for each variable. (Student name is a string, age is an integer and exam result is a double) Now that we’ve defined each variable’s data type in the constructor, when the program takes each instance (individual student) we enter into the code and applies the set of instructions we’ve outlined in the method, it knows what the data actually means! (because of our constructor)
Hope that helps! Probably made more questions than gave answers but hopefully there’s something in there that makes sense. Also please if any of the above is incorrect (which is very well may be) please feel free to point out where I’m wrong. I’m amateur and learning!