Everyone’s giving you part of the answer–which is understandable, programming’s complex.
But I don’t see anyone addressing the contrast between object-oriented and procedural, which is key to understanding object-oriented.
“Procedural” programming is just a list of instructions. Do this, do that, do that, do that, do that. It can get messy, because you have variables all over the place and sometimes it can be hard because you don’t know how much stuff you’ll be working with.
So for example:
string student1Name = “John”
string student1LName = “Smith”
int student1Grade = 95;
string[] student1Classes = {“math”, “science”, “gym”}
string student2Name = “Jim”
string student2LName = “Jones”
int student2Grade = 85
string[] student2Classes = {“math”, “art”, “science”}
//Student 1 drops gym
student1Classes[2] = “”
//Student2’s grades improve
student2Grade = 95
See how that’s going? What about a class of 20 students? A college class of 300?
But what if you put all those variables into a nice little package that you can just tell to do stuff? That’s an object, and that’s the crucial difference between object-oriented programming and procedural programming: you’re both “encapsulating” the data so that only that which really needs to can see it, and also “abstracting” the processes of how to actually do stuff so that you’re not forced to repeatedly do stuff.
An object is a template that represents some logical structure that you can make many copies of, and each copy exists independently of the others. So going back to that example:
Student student1 = new Student();
student1.Name = “John”
student1.LName = “Smith”
Student student2 = new Student()
student2.Name = “Smith”
student2.LName = “Jones”
//Student 1 drops gym
student1.dropClass(“gym”)
//Student 2’s grades improve
student2.adjustGrade(95)
In that second example, it’s most of the same stuff–except that you no longer need to keep track of a thousand variables. All you need to know is student1 and student2, and those packages contain name, lname, grade, a list of classes, etc. You can tell it to do stuff (“dropClass”, “adjustGrade”) without having to worry about the manipulation of these internal variables required to accomplish that.
It greatly simplifies the process.
Latest Answers