Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Polymorphism with an example?

Answer Posted / bilal dooply

Polymorphism is a property in which a single object can have more than 1 form.

Example [Code in VB.NET]:

Class Animals
'Walk() is declared Overridable

Overridable Public Sub Walk()
Console.Writeline ("Walking")
End Sub
End Class

Class Dog
Inherits Animals
'Let us make Dog is walking

'Walk() is overriding Walk() in its base class (Animals)

Overrides Public Sub Walk()
Console.Writeline ("Walking")
'Important: As you expect, any call to Walk() inside this class
'will invoke the Walk() in this class. If you need to
'call Walk() in base class, you can use MyBase keyword.
'Like this

'Mybase.Speak()

End Sub
End Class

Class MainClass
'Our main function

Shared Sub Main()
'Let us define Tommy as a Animal (base class)

Dim Tommy as Animals
'Now, I am assiging an Indian (derived class)

Tommy = new Dogs
'The above assignment is legal, because

'Dogs IS_A Animals.

'Now, let me call Walk as

Tommy.Walk()
'Which Walk() will work? The Walk() in Dogs, or the

'Walk() in Animals?

'The question arises because, Tommy is declared as a Animals,

'but an object of type Dogs is assigned to Tommy.

'The Answer is, the Walk() in Dogs will work. This is because,

'most object oriented languages like Vb.net can automatically

'detect the type of the object assigned to a base class variable.

'This is called Polymorphism

End Sub
End Class

Is This Answer Correct ?    7 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why oops is important?

996


Where is pseudocode used?

1003


Write A Program to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)

4056


Describe these concepts: Polymorphism, Inheritance and Abstraction.

1116


What is abstraction in oops?

957


IS IT NECESSARY TO INITIALIZE VARIABLE? WHAT IF THE INSTANCE VARIABLE IS DECLARED final ? IS IT NECESSARY TO INITIALIZE THE final VARIABLE AT THE TIME OF THEIR DECLARATION?

2002


What is coupling in oops?

1034


What are the 5 oop principles?

1046


What is object in oops?

998


What is difference between polymorphism and inheritance?

997


officer say me - i am offered to a smoking , then what can you say

2024


What is methods in oop?

902


What is an interface in oop?

958


What does and I oop mean in text?

1082


Give two or more real cenario of virtual function and vertual object

2260