GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  Visual Basic
Go To First  |  Previous Question  |  Next Question 
 Visual Basic  |  Question 417 of 453    Print  
How do we achieve polymorphism?

  
Total Answers and Comments: 5 Last Update: March 07, 2007     Asked by: Eswar 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
August 23, 2006 07:47:33   #1  
Nakul Lande        

RE: How do we achieve polymorphism?

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.


 
Is this answer useful? Yes | No
September 15, 2006 03:02:28   #2  
Ashish Tandon        

RE: How do we achieve polymorphism?

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.


 
Is this answer useful? Yes | No
January 09, 2007 04:17:28   #3  
shweta        

RE: How do we achieve polymorphism?

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.


 
Is this answer useful? Yes | No
March 07, 2007 00:59:25   #4  
breathshadow Member Since: February 2007   Contribution: 2    

RE: How do we achieve 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.

 
Is this answer useful? Yes | No
March 07, 2007 01:02:42   #5  
breathshadow Member Since: February 2007   Contribution: 2    

RE: How do we achieve polymorphism?

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.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape