Introduction
In today’s day and age, it is crucial to have a good understanding of what OOP is, because each programming technology makes use of some form of OOP. Today, you will learn what OOP is, and how a good understanding of how the basics work can help you succeed in your programming endeavors.
What Is OOP?
Object-Oriented Programming is a concept used by programmers to develop fully independent units of code. Each independent unit is known as an object. An object can represent anything. I. Repeat. Anything. An object could be a button on your form, your form itself, or a variable used inside your program’s logic.
With OOP, the art is to connect all these objects appropriately. How? Simple. Each object has Properties and Methods. Each object derives from a class. Each object can be used to interact with other objects in the same way. Wait; let me not get ahead of myself. Let me rather start at an appropriate place.
What Are Objects?
When I started teaching programming to my students many moons ago, I initially struggled to explain what objects are to my students. I know it sounds silly, but this article’s aim is to connect the dots for you. I see on a daily basis how people struggle with certain tasks because they do not know the basics.
Now, student, Let me explain what an object is:
An object is something that has properties and methods. Okay, now what does that mean?
Look at a car. What do you see? You see a car. Now, think; what differentiates it from the other cars? Now, apply the same logic to programming. You see a button—what differentiates it from the other buttons? What differentiates a String Variable from an Integer Variable?
Right, I hope you saw what I am trying to show you. Think. Break each part down. Now, before we continue with objects as a whole, let’s move on to the two things that separate objects.
Properties
A property, in ordinary terms, is anything that describes an object. Think of a color. Think of size. Look at the cars again; you will see that there are big cars and small cars. There are new cars and old cars. There are different colored cars. I can break it down further. Some of the cars have six gears, some are automatic. Some cars have more horsepower than others.
In a programming environment, the same basic rule applies: Each object is different. Each button has different text written on it. Some buttons might differ in size. Some variables have a larger capacity than others. Some variables only can store strings whereas some variables only can hold a true or false value.
Methods
The second thing that makes an object is a method. A method is how an object behaves, In other words, a method is what the object can do.
Back to the car analogy: Any car can accelerate, start, break and turn; my car also can always break when I need it the most….
If you look at a button, it already can do something even if there is not one line of code inside it. When you click a button, it appears to be pressed down. Look closely. When you release the mouse button, the button pops up again.
Back to Objects
As you can see, without properties and methods, an object cannot exist. Let me take it further. Although each car is a different shape and size and can do the same things differently, we still refer to them as cars. This is because each car is made of a template, a blue print, and a class.
Classes
A class is from where objects derive their abilities. Although each form, button, and variables are different, they all have a common name. Obviously, with OOP, you can make your own classes with their own properties and methods. Here is a small example:
Public Class Student Dim Student Name As String Dim StudentSurname As String Public Sub GetStudentName As String StudentName = TextBox1.Text MessageBox.Show("Student Name is: " _ & StudentName) End Sub
Here, you have created a new class name, Student. This class has two fields: StudentName and StudentSurname, and one method, GetStudentName, which displays the student name, as simple as that.
Instantiation
Instantiation is the process of instantiating a class. In other words, you create an instance of the class. This simply means that you want to use the class from somewhere. Here is an example:
Dim StudentObject as New Student
Obviously, this has happened from another class. You use the New keyword to create the instance of the class which simply loads the class into memory. After this is done, your StudentObject object contains all the capabilities of the Student class because it represents that class. You may now refer to this class’ methods like so:
StudentObject.GetStudentName
This will show the entered name.
Inheritance
As any person inherits features from their parents, classes can act the same way. You can create a class that inherits its capabilities from another class. Here is an example:
Private Class ProgrammingStudent Inherits Student ' Programming student specific properties ' and methods End Class Private Class WebDesignStudent Inherits Student ' WebDesign student specific properties ' and methods End Class
The above two classes both inherit from the Student class, which contains the Name and Surname. You now can give each of the above two classes specific properties and methods that won’t interfere with the other class. But, they still have the same capabilities as the Student class.
Conclusion
I hope that this short yet powerful article has opened up the doors to OOP for you. Until next time, bye!