Details of Object-Oriented Implementation
Specifying the Class
The class specifier starts with a member access modifier, then the keyword Class, followed by the class name.
By convention, begin class names with a capital letter and use a capital letter for every word in the class name (SampleClassName).
The member access modifier can be Public, Private, Protected, Friend, or Protected Friend.
A key feature of object-oriented programming is data hiding, which means that data is concealed within a class, so that it cannot be accessed mistakenly by functions outside the class.
The primary mechanism for hiding data is to put it in a class and make it Private.
Private variables or methods are accessible only to members of the class.
Public data or methods are accessible from both inside and outside the class. Anyone can access the data or methods and possibly alter it.
Protected is a special modifier used in inheritance.
Friend variables or methods are accessible only within the program that contains the entity declaration.
Protected Friend variables or methods have the union of Protected and Friend accessibility.
Classes that do not specify an access modifier are declared as Friend by default.
If the access modifier of an instance variable or constant is not explicitly declared inside a class, it defaults to Private.
If the access modifier of a method is not explicitly declared inside a class, it defaults to Public.
Public methods can be accessed from outside the class.
Usually the data within a class is Private and the methods are Public.
The data is hidden so it will be safe from accidental alteration.
The methods that operate on the data are usually available outside the class.
To use a method, the dot operator (the period) connects the object name and the method: demoObj.setVar(1066)
This syntax is used to call a method (setVar) that is associated with a specific object (demoObj), instantiated from class DemoClass.
Methods of a class can be accessed only by an object of that class.
Calls to methods are referred to as messages.
The call demoObj.showVar( ) can be thought of as sending a message to demoObj instructing it to display its data items.
Constructor (Sub New)
A constructor is a member sub (method) that has the name New.
The constructor is executed automatically whenever an object is instantiated from that class.
Constructors are used to initialize the instance variables of a class.
Constructors are not required -- instance variables can be initialized implicitly, can be initialized in a constructor of the class, or their values may be set later after the object is instantiated.
Constructors cannot specify return types or return values.
Public Sub New ( <optional parameter list> )
' initialization of instance variables or other instantiation tasks
Arguments are sometimes passed to constructors -- these arguments provide values that the constructor uses when initializing the instance variables.
If a constructor is not defined explicitly in a class, then an implicit no-argument constructor is provided by the IDE.
If you don't explicitly declare a no-arguments constructor but declare some other constructor, you will only get the constructor that you do explicitly define. If you never declared a no-arguments constructor but do define another type of constructor, VB.NET does not insert a no-arguments constructor.
The default constructor initializes the primitive numeric data type variables to 0, Boolean variables to false and references to null.
Multiple constructors can be specified for a single class, that is, constructors may be overloaded to provide a variety of means for initializing objects of a class.
Object declaration: Dim objectName As ClassName
Allocation: objectName = New ClassName( )
All-in-one: Dim objectName As ClassName = New ClassName( )
Finalizer (Sub Finalize)
Each class may include a finalizer method called Sub Finalize that does termination housekeeping on each class object before the memory for that object is garbage collected by the system.
The Finalize method can contain code that needs to execute just before an object is destroyed, such as closing files and saving state information. The system calls Finalize automatically when an object is destroyed.
The garbage collector runs periodically (or can be explicitly called using GC.Collect( )) to reclaim memory for the system.
Before an object gets garbage-collected, the garbage collector gives the object an opportunity to clean up after itself through a call to the object's Finalize method.
Finalizers provide a disciplined way to give resources back to the system when they are no longer needed to avoid resource leaks.
The Finalize method receives no parameters and returns no value.
A class may have only one Finalize method.
Finalizers are rarely used with simple classes.
An example of a task included in a finalizer is the decrement of a static counter.
Overrides Protected Sub Finalize ( )
' statements to return system resources
Note: an object can be deallocated by calling the
Public Sub Dispose( ) Implements IDisposable.Dispose
myWidget = Nothing
' Insert additional code
One benefit of the object-oriented approach is the close correspondence between the real-world things being modeled by the program and the objects in the program.
This makes it easy to conceptualize a programming problem. You figure out what parts of the problem can be most usefully represented as objects, and then put all the data and methods connected with that object into the class specification.
In some situations it may not be obvious what parts of a real-life situation should be made into objects because there are no hard and fast rules. Often you proceed by trial and error. You break the problem into objects in one way and write trial class specifiers for these objects. If the classes seem to match reality in a useful way, you continue. If they don't, you start over, selecting different entities to be classes. The more experience you have with OOP, the easier it will be to break a programming problem into classes.
Some of the benefits of object-oriented programming are probably not apparent at this point because OOP was devised to cope with the complexity of large programs. Smaller programs have less need for the organizational power that OOP provides. The larger the program, the greater the benefit.
For an example, click here.
Please Report Errors in Notes Here