OOP Terminology

Some OOP Terminology

Like any technology, Object Oriented Programming has its own terminology.  Getting on top of this terminology is an important part of the process of understanding OOP.

This list is not set out in alphabetical order, but in a sequence that should be logical if it is read from top to bottom.  This isn’t entirely possible, because of the way things are inter-connected in .Net, but hopefully it makes a little more sense this way. The descriptions are non-technical as far as possible, and therefore should not be taken absolutely literally – there is some form of exception to almost everything described here.  These comments should be used as a starting point for more in-depth investigation.

Class. A class is a template that is used to create an object.  It is a portion of code commencing with Class <classname> and ending with End Class which includes code and data that defines what properties and methods the class has and (if appropriate) how the class displays.  Classes are at the heart of Object Oriented Programming.   The .Net framework includes a huge number of classes that are already defined for you.  You can use these classes, as well as any that you create yourself, in your code.

Structure.  A structure is a template, in much the same way that a Class is a template.  It is a portion of code that starts with Structure <structurename> and ends with End Structure and which includes code and data that define what properties and methods the structure has. Structures are used instead of a Class when you don’t want to do much more than group several data items that have a logical connection, and when the size is small.  An example would be to group the X, Y and Z values of a 3-dimensional point.  Although a Structure can do most of the things that a Class can do, as soon as the properties and methods start to become a bit complex, a Class is probably preferable.  Converting a Structure to a Class is trivial.  You can write a lot of code without ever needing to think about Structures – they are mentioned here because it is important to understand that Classes are not the only kinds of templates.

Instance.  This is the actual object that is created using the template. If I use code such as :

     Dim MyForm As Form1 = New Form1

then myForm is an instance of (the Class) Form1.   If I use code such as

     Dim thisPoint as Point3D = New Point3d(12, 20, 17)

then thisPoint is an instance of (the structure) Point3D. Instance is often used in preference to Object because Object is sometimes used (incorrectly, in my opinion) to refer to the template (the Class, Structure etc). For those new to OOP, this confusion makes things quite difficult, as it is very important to be quite clear about the distinction between the template (Class, Structure) and the objects (Instances) created from those templates.

Instantiation. The process of creating an instance.  Many objects are created through the use of the keyword New, which indicates that the object should be created from a template.  Objects can also be created by cloning an existing object, or, for certain types of objects, simply by declaring a variable for the object.

Type.  A generic term used to refer to the template of an object.  So if myForm is an instance of Form1, we can say that the type of myForm is (the Class) Form1.  If thisPoint is an instance of the structure Point3D, then the Type of thisPoint is (the structure) Point3D.

Constructor. The term used for the process that creates a new instance of a type – a new object.  It consists of internal procedures that look after the actual ‘construction’ of the object, as well as any code that is included in the New sub of the type.

Variable.  The label used in code to identify the particular instance – that is, the particular object.  Not all objects have (or need) a variable, but if you wish to reference the object in your code then you will need a variable for the object.  Variables can be re-used – that is, they can be assigned to new objects.  Variables have a Type, and they can only be used to refer to objects of a matching type.  Creating a variable (which involves giving it a scope, a name and a type) is called a declaration.

Scope. This refers to the portion of code within which a variable is meaningful.  It is determined by where within the code the variable is declared, and by any scope modifier included within the variable declaraton.  Scope modifiers include Friend, Public and Private. The compiler will usually tell you when you have used a variable outside its scope, but there are circumstances where strange errors can occur because of misunderstanding the Scope rules.

Reference variable. This is a variable that is declared as a Type which is a reference type.  Remember – every variable must have a type.  Some types are reference types and some types are value types.  A reference type is distinguished by the fact that when the variable is used within your code, you are actually manipulating a reference to the object, not the object itself. Therefore, the statement

   Dim myForm as Form2 = New Form2

establishes a reference variable (myForm) that can be used to manipulate the object created from the Form2 template.  But the variable is not the same as the object – the variable is only a reference to the object. This has implications for a number of things.  For instance, when you do an assignment

   Dim myOtherForm as Form2
   ‘…
   myOtherForm = myForm

you are not copying the object myForm to myOtherForm, and, most importantly, you are not creating a different or new instance of Form2.  After the above assignment myForm and myOtherForm are two separate variables which refer to the same object.  Having two variables referring to the same object is perfectly legal and quite common.  The problem is that when it occurs it is not always as explict as this simple example, and it can be easy to forget that the two variables refer to the same object.

Value variable. A variable that refers to a type that is a value type.  For all practical purposes, you can regard the variable as the object. So, for instance, when you declare a variable that is a value type:

   Dim I as Integer = 27

you are creating an integer object called I.  If you do an assignment to a different variable:

   Dim J As Integer = 0
   ‘…
   J = I

you are giving a different integer object called J the same value as the integer object called I. Note: Some people are reluctant to refer to instances of value types as objects, and there is some technical basis for this.  But in practice, it seems to me that it is easier to think of everything that you can refer to using a variable as an object, and just remember that value variables are handled slightly differently for some purposes – for instance there is no need for the New keyword to create an instance of a value type.

Static.  A static variable is one that does not disappear between invocations of the method within which it is declared.  Usually, if you declare a variable in a method (a Sub or Function) then that variable is destroyed when the method finishes, and re-created when the method is next executed.  A static variable, however, is not destroyed.  It is created and initialized when the method is first executed, but for each subsequent execution it has the value that it was set to the last time the method finished.  A static variable can often be used where you might have otherwise used a global variable.  Even though a static variable retains its value between invocations of the method, it is still local to that method – the current value of the variable cannot be accessed from outside that method.

Shared. A class is shared if it is not necessary to create an instance of the class in order to be able to use its methods.  For instance, if you use an expression such as “My.Computer.Filesystem….” then you are using an instance of the My.Computer.FileSystem class, but you did not have to create that instance or assign a variable to it in order to use it. You can construct your own shared classes to, in effect, extend the command set of the VB language.

Note: In C# Static means Shared and Shared means Static.

Field.  A variable as used within a Class or Structure.  A field can be private, in which case it is not visible outside the Class or Structure, or it can be Friend or Public so that it is visible outside the class or structure.  As far as the user of an instance of a class is concerned, a Public or Friend field is very similar to a Property.  Structures usually use Public Fields for exposing their variables, while Classes usually use Properties.  Fields have a little less overhead than properties, but they provide the Class designer with fewer options for monitoring and controlling how changes are made to the values.

Property.  This is the principle mechanism by which Classes expose their variables to the outside world.  Properties are values of any type which the designer of the Class decided to create in order to enable code that uses instances of the class to interact with it.  That interaction will be by setting a value for the property (Property Set) or getting the current value of the property (Property Get).  The important design feature of a property is that both the Set and the Get are implemented as methods within the class, so the designer of the class has full control over the setting and getting process.  For instance, code in the class may need to execute whenever the property value is changed – the Property Set method will contain calls to that code, so the property value cannot be changed without that code being executed. Similarly, a value such as a count may need to be calculated in order to be provided to the user.  Instead of continually re-calculating the value so it is always up to date, the calculation can be put in the Property Get routine, and it will only occur when the user actually requests the count value.

Method. A generic term for a function or sub exposed by a class or structure.  A method is the mechanism by which a user of an instance of a class or structure tells that instance to do something.  It might be as simple as returning a value (eg, the ToString method of the String class) or it might involve complex internal routines (eg, the Sort method of the Array class). Note that the function form of the method is almost indistinguishable from a Property Get – they will be implemented within the class using different formats, but they appear to the user of an instance of that class to be almost the same.

Inheritance.  The facility, when creating a new class, of automatically incorporating all the features and functionality of an existing class, and changing only what needs to be different from that other class.  Inheritance helps consistency, in that classes that inherit from a common class will have the same basic features.  Because very few programming problems are brand new, for any reasonably complex class that you are designing there is almost always an existing class that has a lot of the features you need, so inheritance simplifies the process considerably.  Inheritance makes it easy to create a new class from an existing class: it therefore encourages programmers to add new functionality by embedding it within a new class. This approach contributes to encapsulation.  You do not have to use inheritance to create a new class, but many of the classes you create will inherit from some other class.

Encapsulation. The principle that each unit of programming should be as self-contained as possible.  The principle also applies to methods, modules and applications, although obviously the detail will be different.  The more self-contained these components are, the more likely that are to be re-usable in other circumstances, and the less likely they are to be affected by changes that are made elsewhere.  Many of the things in VB .Net that seem more complex than they ought to be are aimed at encapsulating functionality.  If you are writing a simple application for restricted use in a controlled environment and you probably won’t have to come back and change it in furure years and no-one else is likely to be interested in the code you used (for instance to change it, fix it, or re-use it in a new project) then you don’t have to worry too much about encapsulating functionality.

Strong Typing / Strongly Typed.  A method of programmng that provides maximum assistance to the compiler in ensuring that you are not doing invisible conversions between different types.  An example is the use of a List(Of.  Because the declaration of the instance requires that you specify the type (eg, List(Of Integer)) you are providing the compiler with everything it needs to be able to warn you about attempts to put something other than an object of the correct type (ie integer) into the list.  Strong typing helps to make you programs more robust.  It does not mean that you cannot or should not convert objects from one type to the other.  It simply means that you should write your code so that any such conversions are explicitly coded (eg, using CInt to convert a floating point number to an integer), and are therefore immediately obvious without the need to understand what the compiler might be doing quietly in the background. Making sure that Option Strict On is included at the start of each source code file is the simplest step you can make towards strong typing.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s