Sunday, July 5, 2009

Polymorphism in object-oriented programming

posted by Sukma Yuliardiana(124.07.1049)
Type polymorphism in object-oriented programming is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B. In weakly typed languages types are implicitly polymorphic.


Operator Overloading the numerical operators +, -, /, * allow polymorphic treatment of the various numerical types: integer, unsigned integer, float, decimal, etc; each of which have different ranges, bit patterns, and representations. Another common example is the use of the "+" operator which allows similar or polymorphic treatment of numbers (addition), strings (concatenation), and lists (attachment). This is a lesser used feature of polymorphism.

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run time (this is called late binding or dynamic binding).

The different objects involved only need to present a compatible interface to the clients (the calling routines). That is, there must be public or internal methods, fields, events, and properties with the same name and the same parameter sets in all the superclasses, subclasses, and potentially interfaces. In principle, the object types may be unrelated, but since they share a common interface, they are often implemented as subclasses of the same superclass. Though it is not required, it is understood that the different methods will also produce similar results (for example, returning values of the same type).

Polymorphism is not the same as method overloading or method overriding.Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class. Method overloading refers to methods that have the same name but different signatures inside the same class. Method overriding is where a subclass replaces the implementation of one or more of its parent's methods. Neither method overloading nor method overriding are by themselves implementations of polymorphism.

One way of doing polymorphism is through the definition and implementation of a common interface. Consider the example below, where two subclasses (Cat and Dog) implement the IAnimal interface. Two Cat objects and one Dog are instantiated and given names, and then they are gathered in a list and their talk method is called.

Namespace std

Public Interface IAnimal
ReadOnly Property Name() As String
Function Talk() As String
End Interface

Public Class Cat
Implements IAnimal

Private mName As String

Sub New(ByVal name As String)
mName = name
End Sub

Public ReadOnly Property Name() As String Implements IAnimal.Name
Get
Return mName
End Get
End Property

Public Function Talk() As String Implements IAnimal.Talk
Return "Meow!"
End Function
End Class

Public Class Dog
Implements IAnimal

Private mName As String

Sub New(ByVal name As String)
mName = name
End Sub

Public ReadOnly Property Name() As String Implements IAnimal.Name
Get
Return mName
End Get
End Property

Public Function Talk() As String Implements IAnimal.Talk
Return "Arf! Arf!"
End Function
End Class

Public Module TestAnimals

' Prints the following:
'
' Missy: Meow!
' Mr. Bojangles: Meow!
' Lassie: Arf! Arf!
Public Sub Main()
Dim animals(2) As IAnimal
animals(0) = New Cat("Missy")
animals(1) = New Cat("Mr. Bojangles")
animals(2) = New Dog("Lassie")

For Each a As IAnimal In animals
Console.Out.WriteLine("{0}: {1}", a.Name, a.Talk)
Next a

End Sub
End Module

End Namespace

0 comments:

Post a Comment