Anda di halaman 1dari 6

Object Oriented Programming Language Features Inheritance, Polymorphism This article describes the basic features involved in Object

Oriented Programming (OOP) languages in general and how they are implemented in Visual Basic. t is by no means an all inclusive! definitive reference but does serve to describe some of the typical features considered to comprise an object oriented language. t may be a good starting point for less e"perienced programmers. Object oriented features are typically lumped into # categories. The first includes the biggies$ Polymorphism! nheritance and %ncapsulation (P %). The second includes other features such as operation overloading! parameteri&ed constructors and class'level attributes and operations. Inheritance Inheritance comes in two forms. Interface and Implementation inheritance. Interface inheritance is available in Visual asic since V !"s introduction of the Implements #eyword. Implementation inheritance is available as of V .$%& with the Inherits #eyword. &he main difference between the two inheritances is that interface inheritance should specify only a contract of desired behavior. It should not allow any corresponding implementation code. It is up to other classes, which reali'e that interface, to provide the implementation. Implementation inheritance lets subclasses share common code and attributes (properties). *lso, implementation inheritance"s ability to declare an operation abstract enables it to act similar to interface inheritance and force subclasses to implement the operation instead. * V .$%& class can only enter into an implementation inheritance relationship with one superclass (also called base or ancestor class). +owever, V .$%&, li#e V !, lets that same class enter into as many interface inheritance relationships as it chooses. *n e,ample may help. -ay you are developing an app in which the Store object wants to as# an instance of the Stereo class to calculate availability inventory. +owever, the -tereo class wants to borrow the functionality already provided by its superclass, Product. &he -tore object might implement such functionality li#e this.
Private myStereo as Stereo Private Function getInventory() As Integer getInventory = myStereo.calcInventory() End Function

&he real implementation of this behavior is in Product"s calcInventory() method. ecause V ! doesn"t support implementation inheritance you need to put some code in the -tereo class.
Implements Product Private myProductObject as e! Product Private Function Product"calcInventory() As Integer Product"calcInventory = myProductObject.calcInventory() End Function

V ! allows the interface, the Product class, to implement the actual behavior. -tereo #eeps an instance of Product (containment) then as#s that reference to do some wor# for it (delegation). &his type of interface inheritance is not a true interface because it allows you to add code to -tereo to provide the actual behavior. /ith V .$%& you can remove the containment and delegation code by using implementation inheritance. For e,ample, in the -tereo class.
In#erits Product $ $ any uni%ue Stereo code $

&he -tore class ma#es a calculation re0uest similar to the way the -tereo class ma#es this re0uest, but -tereo carries out the wor# much differently in V .$%&. &he -tereo instance, my-tereo, doesn"t contain a calcInventory() method so V loo#s up its superclass, Product, and e,ecutes its calcInventory() procedure. Implementation inheritance also allows you to override superclass operations. *n Items class might use this to provide its own implementation of calcInventory() which would cancel out the behavior provided by its Product superclass. For instance.
Overrides Function calcInventory() As Integer $ $ any uni%ue Items code $ End Function

*ll of the above may sound a bit confusing if you have not used inheritance in the past. &a#e a second to reread the above and you will see that it is really not that difficult. Interface inheritance tends to be a little harder to grasp for some but, again, it is not that though of a concept. /hen to 1se Inheritance Implementation inheritance allows you to reduce your code base drastically since subclasses gain the methods of their superclass. &his is a mi,ed blessing. If you start

with a poor design you will end up with code that is more difficult than ever to debug and maintain. 2ode and attributes once common in the superclass may not remain common as the app"s business needs evolve over time. %ventually many subclasses may end up overriding the behavior of the superclass. /orse, the subclass may override the superclass, do its own wor#, then call the same operation again on the superclass. elieve me, I"ve seen it happen in other languages. V .$%& re0uires you e,plicitly use the Overrides and Inherits #eywords. 3ost languages do not mandate this and are smart enough to #now on their own. I believe this is a good thing. It provides self documentation and instant visual clue as to whether a method is overridden or not simplifying maintenance and debugging. 1se Interface Inheritance /hen.

&he base or superclass represents a generic facility such as a table loo#up, uni0ue algorithm,... &he number of common operations is small. &he base class has few, if any, attributes. 2lasses reali'ing the interface are diverse, with little or no common code.

1se Implementation Inheritance /hen.


&he base class is an entity class (business or domain class) of primary interest to the application (not a utility or control class). &he implementations are comple, with a large number of operations. 3any operations and attributes are common across the subclasses.

Polymorphism Polymorphism means many forms. It gives classes the ability to define the same operation but provide uni0ue implementations of that operation. In other words, each of the class" methods is different. 4ou can use polymorphism with either interface or implementation inheritance. Polymorphism is more than overriding a superclass"s operations which is a small part of polymorphism"s power. For an operation to be polymorphic it must implement an operation defined in an interface it implements or it must implement an operation defined in a superclass. &he superclass" operation is typically abstract, which means it has no implementation code. For e,ample, you might encounter the getInformation() operation defined in Product as abstract. If another object refers to either a -tereo or Items object and wants to send it a message, it is best not to hard code that class name.

Public Function getProductIn&o(myProduct as Product) As 'oolean myProduct.getIn&ormation() End Function

&he input parameter"s data type is Product. V is smart enough to #now whether the function is as#ing -tereo or Items to get its information. %ncapsulation %ncapsulation is also called information hiding and ensures that no other class has #nowledge about attributes and5or behavior unless the class publishes them e,pressly. V lets you declare attributes and behaviors with the Private #eyword inside a class module. &his hides them from the world outside of the class. 2onversly, if declared with the Public #eyword other objects can access those properties and methods yet #now nothing of their actual implementation. Overloading Overloading lets a function vary its behavior based on its input parameters. V .$%& lets you have multiple functions with the same name but with different input parameters. &he language #nows which version to e,ecute based on what is pased in. *n e,ample.
Overloads Function calcInventory() As Integer $ $ any code uni%ue to t#is no parameter version. $ End Function Overloads Function calcInventory(dte(ate as (ate) As Integer $ $ code uni%ue to t#is date driven version. $ End Function Overloads Function calcInventory(Start(ate as (ate) End(ate as (ate) As Integer $ $ code uni%ue to t#e date range version. $ End Function

-hared 3embers -ometimes you need to provide behavior on behalf of all objects in a particular class and maintaining attributes that all instances of a class can have access to. V .$%& uses shared members to resolves this issue. Other languages call these feature static or instance operations.

If the Product class needs to maintain an attribute and operation that are available to all of its instances, you might implement the Product class li#e this.
S#ared *otalProduct+ount As Integer S#ared Function calc*otalProduct+ount As Integer $ $ get count o& all products. $ End Function

&hese attributes do not re0uire an object instance to use them. In V ! you can create a variable in a .bas module that is acessible from all objects of a given class. &his achieves the same function but is not very object6oriented.

7lossary 2onstructors (onstructors allow you to create an object and provide it with an initial set of data so it can initiali&e properly. )ithout constructors! you create an object! then need to all a separate method to initiali&e it. Two steps. %ncapsulation s data hiding. *ou can create a set of procedures or methods and properties that form an interface. Other code can then use these methods with any +nowledge of the code within the methods. The procedures or methods you write is called an implementation. The implementation is encapsulated within the interface. Inheritance The concept that an object can gain the interface and actual behaviors (implementation) of another object! then e"tend that interface or those behaviors. ,ay you create a generic Product object that handles things common to all your products. -rom it you may create speciali&ed Perishable and .on'Perishable objects. Both objects inherit the original Product object/s interface and behaviors but can e"tend or change some of those behaviors. Initiali'ers 0n initiali&er allows you to declare a variable and assign it an initial value all in one statement. Object6 ased This loosely describes a language that interacts with objects easily and directly. Object6Oriented Object oriented languages must support polymorphism! inheritance and encapsulation

(P %). Overloading This allows you to declare multiple procedures with the same name in the same scope each having different input parameter specifications. -or instance! you many define a function (reateTotal that totals the values in its array argument and another (reateTotal function that ta+es 1 long arguments and returns their sum. *ou call (reateTotal and pass it the proper parameters and the language will +now which version of the function to use. Overriding )hen using inheritance your new class gets all the methods from its parent or super class. 2owever you may want a different implementation for one of these methods. *ou do so by overriding the original (inherited) method with your own code. *our new code may even call the original method in the parent class. Polymorphism This is the ability to have two different objects of two different types both implement the same method. t lets you write code that calls that method regardless of which type of object is in use at the moment. -hared 3embers Otherwise +now as class! static or instance members. ,hared members are methods or variables that are e3ually available to all instances of a class. %very object that you create! based on a given class! shares these same variables and routines. 1ser Interface Inheritance 4eans you can create a VB form template then derive all your other forms from this template. 0ll other forms will inherit the loo+ and code from the template form. 0 change to the original form will propagate out to all the child forms.

Anda mungkin juga menyukai