Simply put, a class is a created object . In more detail, a class is a reference type (that which stores some type of information) that encapsulates (hides or encloses) and or modularizes (making for flexible, varied use) any amount of methods (functions or subroutines), fields, variables or properties or constructors (object creating function) that classifies data and performs some kind of functionality. Thus, the focal point of OOP is to classify all common functionality and members within a main class that when utilized or instanced in your application, it becomes an object . Object creation is achieved by using the new keyword in your code, as we'll see.
The examples we'll mostly focus will revolve around the House
class and its key traits/characteristics. Now if we had a house class and it contained its pertinent house characteristics ( fields or variables) it would look something like this. So go ahead and place your preferred language code section in a text file. Next save it as House.cs
if using C# or House.vb
for VB. This is your source file that you'll compile in the next section.
[C#]
public class House {
// Its members
// This is known as initializing a field - see the "Quick Structs" section further down
// public string HseColor = "gray";
public string HseBuilder;
public string HseSize;
public string HseColor;
public string Summary() {
return HseBuilder + " is building a new house that's " + HseSize + " and is painting it " + HseColor;
}
}
[VB]
Public Class House
Public HseBuilder As String
Public HseSize As String
Public HseColor As String
Public Function Summary() As String
Return HseBuilder &" is building a new house that's " & HseSize &" and is painting it " & HseColor
End Function
End Class
The bits of information we include above in our House blueprint is who the builder is, the house's size and its color. Further enhanced by a summary entailing overall what's included and taking place within our House class.
Therefore, even though this is one template describing one house, it could easily represent and be utilized for any number of houses since each of its public properties values or characteristics may be different each newly created instance of it. In the example above, our diverse information is contained in our public variables or fields that hold what we pass to them in memory. Our variables and method string declaration is known as a data type. In VB the As
keyword specifies the data type, in C# the data type precedes the variable name and or method.
The term for using a class, as many times as you wish, each time with different characteristics, is called instantiation or creating an object for use. So the first time the house class is instantiated and assigned it can hold the properties for example, HseSize of "Big" and an HseColor of "Red", and when re-instanced could hold some other very different information. So out of our one House class, we've build two houses all with different characteristics; one class - many uses.
Variables as we've seen have the ability to hold one set value. Class properties unlike their close relatives variables, as we'll examined a little further down, extend the functionality and pretense by allowing you to carry out conditional checking within them preventing any faulty values passed in. Therefore, you can do much, in turn furthering the nature and objective of OOP, as we'll soon examine in the next section.
All we've discussed still needs to be demonstrated real world. Therefore, we'll now build two houses, each with its own particular qualities. So here goes.
Comments