Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey everyone

I am having trouble wrapping my brain of how to code a prject with inheritance polymorphism etc. I understand the concept but not the code.... I need a VISUAL representation of how to do this... 4 classes,- 3 constructors, methods, overriding etc.. any sites that are good? Again a VISUAL diagram - something help?! I am a visual person in a non visual world right now

 

Rob

Posted

Fun With Patterns

 

Check out this site, it's one of the best for a quick rundown on various patterns.

 

http://www.dofactory.com/Patterns/Patterns.aspx

 

As far as understanding how basic inheritance works, imagine the following. You have a Class Bob, containing a public method HasAGoodJob(){...}.

 

You decide you want another class similar to bob, with a little extra functionality along with everything Class Bob has, so you create Class Ron inheriting from Bob, with the only addition being the public method HasAHotGirlfriend(){...}.

 

Now Class Ron automatically contains the method HasAGoodJob(){..} without ever having to explicitly declare it, and he also contains the additional method HasAHotGirlfriend(){...}.

 

Class Bob doesn't have access to the HasAHotGirlfriend() method, but no matter what, the compiler knows that he will have the HasAGoodJob(){...} method. This means that once necessary, Class Ron can be cast into the type Bob without any worries, every shared method/property contained within Ron will be preserved, while anything exclusive to Ron will be lost. Unfortunately, the result of the conversion means that Ron will no longer be Ron, and as such he will no longer have the method HasAHotGirlfriend(){...}.

 

Personally, I find interfaces easier to use for casting purposes, but they tend to be a little more difficult to wrap your head around. I mostly use straight inheritance when I don't plan to use the parent class, but want to split up specific methods amd functionality among many different classes/objects.

 

For example, if I am creating 100 classes to represent different animals, I wouldn't want to create the functionality representing the traits of a Reptile, Mammal,Amphibian for each individual class. Instead, I would create the 3 classes Reptile/Mammal/Amphibian, and have each class of animal inherit from whichever one is appropriate. It really helps to modularize your code and speeds up development greatly.

 

A good rule of thumb is to look at your code and decide if it's bulky and hard to modify. If so, you probably aren't using property OOP techniques. Elegant OOP makes code modification simple and seamless, if you find otherwise, you should get out your pen and notepad to do a little re-designining.

 

Hope this helps a little, if you made any sense out of what I just said and want any more pointers let me know.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...