Learn to Code via Tutorials on Repl.it!

← Back to all posts
Polymorphism tutorial c#
Bunnytoes

Polymorphism c

we start by making a parents class Animal

make sure to have this as a virtual void or it won't work
next we use the class animal we made and have pig be the child

make sure this is override so that it can override the other thing. Repeat that with a different animal how many times you want.

now for the last part, we have the basic setup code

then we make a Animal called what we want

the full code is

and that's it! The output will be
The animal makes a sound
The pig says: oink oink
The dog says: woof woof
The cat says: meow

hope this was helpful and bye

Voters
Bunnytoes
Comments
hotnewtop
Highwayman

Is this a tutorial o for learning what polymorphism is or just how it's done in c#?

Bunnytoes

@Highwayman it shows how it's done, because if you don't have the void as override it won't work, it shows you how to do it

Highwayman

oki cool. @Bunnytoes