regulus said:
I write my base class and leave it be to do its "thing". I inherit that class into another class with EXACTLY the same properties to which I can add or change as necessary. It's essentially the same class but this time changed a bit to accomplish something else.
Almost. The useful thing about deriving from a super class is that you don't have to re-implement all of the methods and properties that were exposed by the base class; you just ignore them and they're still available in the sub class. This leaves you free to modify/add to the functionality available through the class. Also, you're not changing it to accomplish something else; strictly speaking, you're
adapting it to accomplish something more
specific, or in a more specific way.
regulus said:
I understand the concept well (hopefully

), but I cannot immediately see the value of inheriting from the base class. Why not just load all the attributes, variables and methods into the base class and call them from there? Or am I understanding this wrong?
Well, a good example of inheritance in C# (there are actually thousands) is the
Stream class and its derivatives.
Stream is abstract, meaning that it cannot in itself be instantiated; all of the methods it specifies are not actually implemented in the Stream class—the methods have no actual code bodies to them, just a semi-colon at the end of the line. Instead, in order to use it, you must create a sub class of it that instantiates all of the abstract method and property definitions and instantiate that sub class.
For example, the
MemoryStream class derives from
Stream and implements the base methods in its own particular way based on what it does (writes and reads data to/from an array of bytes). Because it implements all of the methods that were specified by
Stream, it can be used by any method that accepts a Stream as a parameter. The method doesn't care about how the particular version of the
Stream it's been given works, it just uses it. You could pass it any one of the
Stream sub classes, for example,
FileStream,
NetworkStream, etc.; they all have their own implementations of the different methods and properties specified by
Stream itself.
Here's an example of an almost useless method that writes a single byte to a stream:
Code:
public void WriteByte(Stream stream, byte value)
{
stream.WriteByte(value);
}
Since this method will take any object that either is a
Stream or derives from it, the following two lines of code are both equally valid:
Code:
WriteByte(new MemoryStream(), 10);
WriteByte(new FileStream("test.bin"), 10);
The
WriteByte method doesn't care how the
Stream object is writing the byte, it just cares that it will do
regulus said:
edit: Inquisitor, you write very well but you get very technical in using a lot of jargon. I understand 100% what you're saying and you say it well, but if a complete newbie was reading that he would run away screaming.
Daven, you are more beginner friendly!
I did try to offer a bit of insight into the reasoning and theory behind object oriented programming as well, though, rather than just giving a description
