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

What is abstraction and encapsulation?

563


What does sksksk mean in text slang?

1511


explain sub-type and sub class? atleast u have differ it into 4 points?

1828


What is polymorphism what is it for and how is it used?

570


What is debug class?what is trace class? What differences are between them? With examples.

1598






What is persistence in oop?

657


What is polymorphism give a real life example?

550


What do you mean by abstraction?

603


Will I be able to get a picture in D drive to the c++ program? If so, help me out?

1648


#include #include #include #include void insert(char *items, int count); int main(void) { char s[255]; printf("Enter a string:"); gets(s); insert(s, strlen(s)); printf("The sorted string is: %s.\n", s); getch(); return 0; } void insert(char *items, int count) { register int a, b; char t; for(a=1; a < count; ++a) { t = items[a]; for(b=a-1; (b >= 0) && (t < items[b]); b--) items[b+1] = items[b]; items[b+1] = t; } } design an algorithm for Insertion Sort

2157


what is graphics

2001


What is abstraction with example?

594


Templates mean

1581


What is overloading in oop?

568


What is polymorphism and types?

593