Tutorial site for inhertiance/poly/abstract

BlueOysterCult

Well-known member
Joined
Oct 3, 2003
Messages
84
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
 
Fun With Patterns

Check out this site, its 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 doesnt 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 dont 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 wouldnt 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 its bulky and hard to modify. If so, you probably arent 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.
 
That is great information!
Thank you for the effort you put into my question - I am going to work on my project today and I will let you know how it goes!
R
 
Back
Top