How do we achieve polymorphism?

Showing Answers 1 - 11 of 11 Answers

Nakul Lande

  • Aug 23rd, 2006
 

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.

  Was this answer useful?  Yes

Ashish Tandon

  • Sep 15th, 2006
 

Overload and override uses the concept of polymorphism

polymorphism is the concept of using multiple functions or methods having same name and different signatures and access modifiers

overloading is for the same class for functions and methods where as overiding is for derived class.

  Was this answer useful?  Yes

shweta

  • Jan 9th, 2007
 

ex. 1. public function add(int a,int b)

               .......

          end function

       2. public function add(float c,float d,float e)

                ......

           end function

when u call add(5,4) then function 1 is called & when add(5.5,4.5,16.7) then function 2 is called.

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes


a little correction Below

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.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions