Polymorphism is just as function Overloading. You can achieve Polymorphism by defining two different functions in the same class with same name but different parameters.
Note:- The return type of the function do not play any role in polymorphism.
in simple Visual Basic 6 we can achieve Polymorphism using Interface.
suppose the requirement is contacting a user on his PSTN (phone) or Mobile or Fax.
we have Phone Mobile Fax Objects. As we know they all have to dial a number to initiate the calling process. We define an Interface called INotifier which has a method named DIAL. All 3 objects implements this iNotifier Interface and implements the Dial method according to the Device itself (just asume they might have different ways of dialing out according to the company policy).
Now we also have a Utility Object which actually initiates the Dialing. We call it the Notifier Object.
Notifier Object also has a method called
public Class iNotifier public sub Dial(Number as string) End Class
public class Phone implements iNotifier sub iNotifier_Dial(Number as string) 'Dialing code end sub end class
public class Mobile implements iNotifier sub iNotifier_Dial(Number as string) 'Dialing code end sub end class
public class Fax implements iNotifier sub iNotifier_Dial(Number as string) 'Dialing code end sub end class
Public class Notifier public sub Dial_a_Device(Device as iNotifier Number as String)
Device.Dial(Number) '<- This is the Polymorphism doesn't mater which object is passed here it will call the related Dial method
end sub end class
**************************** GUI Form
sub Phone_Button_Click() dim Notifier as new Notifier dim Phone as New Phone
Notifier.Dial(Phone "123455633")
end sub
because we can pass any object (Phone Mobile or fax) as they all implements the iNotifier interface. They all have implemented the Dial method as per their Needs.
in simple Visual Basic 6 we can achieve Polymorphism using Interface.
suppose the requirement is contacting a user on his PSTN (phone) or Mobile or Fax.
we have Phone Mobile Fax Objects. As we know they all have to dial a number to initiate the calling process. We define an Interface called INotifier which has a method named DIAL. All 3 objects implements this iNotifier Interface and implements the Dial method according to the Device itself (just asume they might have different ways of dialing out according to the company policy).
Now we also have a Utility Object which actually initiates the Dialing. We call it the Notifier Object.
Notifier Object also has a method called
public Class iNotifier public sub Dial(Number as string) End Class
public class Phone implements iNotifier sub iNotifier_Dial(Number as string) 'Dialing code end sub end class
public class Mobile implements iNotifier sub iNotifier_Dial(Number as string) 'Dialing code end sub end class
public class Fax implements iNotifier sub iNotifier_Dial(Number as string) 'Dialing code end sub end class
Public class Notifier public sub Dial_a_Device(Device as iNotifier Number as String)
Device.Dial(Number) '<- This is the Polymorphism doesn't mater which object is passed here it will call the related Dial method
end sub end class
**************************** GUI Form
sub Phone_Button_Click() dim Notifier as new Notifier dim Phone as New Phone
Notifier.Dial_a_Device(Phone "123455633")
end sub
because we can pass any object (Phone Mobile or fax) as they all implements the iNotifier interface. They all have implemented the Dial method as per their Needs.