Using Objects in ASP.NET: Part 1/2 - Some of the Grander OOP Features (Page 2 of 6 )
Encapsulation – Objects are by nature, encapsulated. That is, only certain portions of their fields and methods are publicly accessible from other classes and one doesn’t really need to know the inner workings of an object in order to use it. Encapsulation has certain benefits. By hiding the inner workings of a class, it makes that class simpler to use because the developer only sees and has access to what is necessary to use the class. Furthermore, the developer cannot accidentally override the data inside that class.
Inheritance – Often considered the "Holy Grail" of programming inheritance is just as often misused or misunderstood. Inheritance defines a particular ability of classes to inherit the data members and functionality from other more generalized classes. The new or "child" class can than add more specific functionality that is parent does not possess.
Polymorphism – Polymorphism is even more difficult to understand and explain. Literally meaning "many forms" polymorphism is the ability for related classes to have the methods or functions with the same name and syntax but different implementations.
Aggregation/Composition – Although these two terms are often used interchangeably, aggregation is more generalized term referring to groups of objects being used to make up another object, much the way a car is made of various parts. Composition is a much more specific form of aggregation where one the pieces can only belong to one whole and outside of that whole, they no longer exist. Usually though, you’ll hear people refer to composition in the more general sense that aggregation defines.
Interfaces – Perhaps one of the most mysterious features of OOP languages, interfaces have very specific purposes, especially in component development. Classes that inherit interfaces are required to have certain methods, fields and/or properties defined and implemented. This allows related classes to have a consistent manner of using them. For example, all the .NET collection classes inherit from the IEnumerator interface, forcing them to have the methods MoveNext(), Reset() and Current property.
Fast Track to Objects If you’ve been programming in procedural or scripting languages for a very long time, or if you are new to OOP then it might be useful to review how to build OOP applications. In OOP languages, programs are defined primarily through a construct known as a class. A class in turn is made up of two things: data, and behavior on that data. Think nouns and verbs. The data (or nouns) in a class are variables known as fields or data members. These are similar to fields in a database record. The behavior is defined by methods – the functions and procedures that operate on the data and make things happen (this would be the verbs.) Combining these two pieces essentially creates a class. A class in turn usually doesn’t operate on its own. Instead, it gets brought into existence through code by other classes. This is known as instantiating the object. The object is brought into existence as a variable reference, its fields get populated with data and its methods get called upon to operate on that data.
In .NET, nearly everything is defined as a class and when used, becomes an object. Even relatively simple values, such as strings and integers for example, are actually instantiated objects. Similarly, more complex items like DataSets, ArrayLists and even Web Forms themselves are all defined by the .NET framework as classes and get initialized (or instantiated) as objects. A true object-oriented language has all or many of the features we looked at above. Since ASP.NET uses the full version of the .NET framework and .NET languages such as C# and VB, you have the ability to use these features in your applications.
Creating Classes To use these features, you are going to have to think and code in terms of classes. Where both VB 6 and C++ allow you to write as OOP or non-OOP as you desire, .NET is designed to more consistently encourage (or force) you to write your applications as groups of classes. To declare a class in VB.NET, most of the time you would use the following:
Public Class ClassName ... End Class
This creates a public class, i.e., a class that can be used by any application not just the one it was written for. Most classes are declared as public. We’ll discuss what this means shortly. Also, it is important to note that you can declare multiple classes within the same file, but most of the time, it is a better practice to limit one class per file. I also usually make the filename the same as the class name. So if I named my file books, the class it contains should be Books.vb (or Books.cs if you are using C#.)
To use a class in VB.NET, you instantiate it. This initializes the class and creates an object based upon it. You can give each object any name and create multiple instances of the same or different classes in your code. Instantiating a class is done using the New statement:
Dim ObjectName as ClassName ObjectName = new ClassName(Optional Parameters)
However, it is important to note that even simple values such as String’s and Integers are classes as can be seen through the following code snippet:
Dim email As String email = "buygreatbooksatwrox.com"
If email.IndexOf("@") < 0 Then Response.Write(email + " is a bad email address.") End If
In the code above, I declare the variable email as a string and give it a default value of "buygreatbooksatwrox.com". The syntax isn’t much different than how I would declare a string in a procedural language. Yet, email is itself an object of the string class, even though we haven’t defined it as such. I can use the built-in IndexOf method on it to look for an "@" symbol and determine if this is a valid email address (in a sloppy fashion, I admit).
Finally, you should be aware that every class you create automatically inherits from at least one class in the .NET framework: the Object class. Remember that I mentioned earlier that inheritance means that one class inherits members (fields, methods and properties) from another. So all classes start out with a tiny bit of functionality built in.