Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[C#] Inheritance and virtual methods

This tutorial will explain inheritance and virtual methods and how to use them in C#(im assuming you have a basic knowledge of oop concepts). Inhertance is when a class uses another class has a base and aquires all its variables and methods, take this class for example:
Code
class Derp
    {
  public int var;
  public void Write(string text)
  {
    Console.Write(text);
  }
    }
this next class uses that one a base( it inherits from it)
Code
class Herp : Derp
    {
  public void WriteLine(string text)
  {
    Write(text + "\n");
  }
    }
[IMG]http://i37.[censored].com/fxax5u.png[/IMG]
As you can see the class Herp contains the method and varible from its base class as well has the other method we added to it.



Virtual methods are a way of changing the behavior of inherited methods:
Code
class Derp
    {
  public virtual void DoWork()
  {
    Console.WriteLine("Doing base class work");
  }
    }

Code
class Herp : Derp
    {
  public override void DoWork()
  {
    Console.WriteLine("Doing inherited class work");
    base.DoWork();
  }
    }
As you can see the inherited class is overriding the virtual method DoWork, the line 'base.DoWork();' calls the original DoWork method from the base class so the output of DoWork from the class Herp would be:
Doing inherited class work
Doing base class work

please leave a comment if you liked this tutorial or if you have any problems or questions about it
Very nice tutorial. Not the biggest but it's straight to the point and informative. Nice one, and rated 5 stars 😝
(Aug 13, 2010 11:41 AM)laptopguy Wrote:  Very nice tutorial. Not the biggest but it's straight to the point and informative. Nice one, and rated 5 stars 😝

thanks, i tried to be a clear as possible
the problem with this kind of subjects is that it is kinda hard to explain when to use it. I'm working on a polymorphism tutorial but I can't think of some good examples when to use it on a way people understand it :P

Same for you you didn't mention when and why you should use it
(Aug 13, 2010 12:01 PM)enacje Wrote:  the problem with this kind of subjects is that it is kinda hard to explain when to use it. I'm working on a polymorphism tutorial but I can't think of some good examples when to use it on a way people understand it :P

Same for you you didn't mention when and why you should use it
I use this alot for games, but polymorphism is a tricky one 😧
(Aug 13, 2010 12:09 PM)killerdoob420 Wrote:  I use this alot for games, but polymorphism is a tricky one 😧

Could you give an example in how to use this in a standard Windows Application ? Like what is it's values ?
(Aug 13, 2010 12:10 PM)laptopguy Wrote:  Could you give an example in how to use this in a standard Windows Application ? Like what is it's values ?
well i guess you could use it for user profiles or something, i can code and actuall example if you like
(Aug 13, 2010 12:29 PM)killerdoob420 Wrote:  well i guess you could use it for user profiles or something, i can code and actuall example if you like

Sure, that would be good mate.
Well the typical example of the usecase is in multiple implementations of things that act alike:

e.g. 5 different types of buttons...
e.g. 5 different commands called from a single parser.

Make a bassclass IrcCommand.

Make a derived class IrcCommandScan
Make a derived class IrcCommandDownload

Make a string map to IrcCommand reference and register your implementations there. Makes it easy to have your commands extendable without having a huge if then else clause...
bringing some of my old tuts up
                                                      
Constructor Can't Be Inherited Why


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by zNinjaApr 15, 2020 03:21 PM 0 117 Apr 15, 2020 03:21 PM
Last Post: zNinja
[C#] by JuicyProgrammingJan 7, 2017 03:23 PM 2 159 Jan 7, 2017 03:23 PM
Last Post: JuicyProgramming
[C#] by severosAug 27, 2016 02:44 AM 4 172 Aug 27, 2016 02:44 AM
Last Post: severos