What is inheritance?
In previous years of programming the developer would often create a class that would contain specific members (properties and methods). When the developer wanted to create another class that would cater to different specific needs, even if they needs were similar in many areas with enough differences to not allow use of the first class, the developer would need to create another class, including all of the members, to fill these needs.
As Object-Oriented Programming (OOP) had caught on the idea of the ability to re-use code was a major benefit of this new concept. To enable to developer to avoid from having to recreate a class with minor changes Microsoft (and other language specific providers) had introduced the idea of “inheritance” of classes. A developer could create a second class (the derived class) based on the first class’ (base class) members. In many cases the derived class would have proven error-free methods and properties the developer would want to bring into the derived class. The developer was then given the option to use “inheritance” of the base class in the derived class. The developer would then be able to pick and choose what members the derived class would be allowed to have access to, as well as what members the derived class would utilize.
Another nice feature was that the developer could create a derived class from the original derived class, any inherited members of the derived (second derived) classes are considered to be “indirect base class” inheritances. The inheritances found in the first derived class are class “direct base class” inheritances.
How do you inherit?
To derive a class you simply follow the class identifier with a colon and then the name of the base class.
Figure 1 shows the creation of the base class called “Base_Patient.cs”:
using System; namespace inheritance_1 // default values constructor class // preferred constructor method //destructor of object class //property firstName of patient //property lastName of patient //Displaying patient's name (last, first) }//end class |
Figure 2 shows the creation of the derived class called “Derived_Patient.cs”:
using System; namespace inheritance_1 //Here we create our constructor for the derived class //Here we created our destructor of the class //Here we are creating the property and the method of setting / getting //Here I am creating a simple console writing statment that will display }//This ends class |
Figure 3 shows the creation of the “Program.cs”:
using System; namespace inheritance_1 //The following lines of code will demonstrate how to call the methods from //You can feel free to move the lines of code around in your own implementation |
You can see from above an example of how you can inherit from another class and how you can still use the base class’ members. The main point to understand here is that when you inherit from a class you are not necessarily overwriting that class; there are other keyword modifiers that can be used with the creation of classes that can force the developer to overwrite a class’ member, and likewise there are keyword modifiers that can prevent a developer from overwriting a class’ member.
The next blog will touch on “polymorphism”; which is one of the methods used to define or redefine the base class’ members (typically methods).
Hopefully this will help you to grab a good grasp on inheritance and how to inherit from a class. Remember that trial and error are the best way to learn in programming, regardless of the amount of books and blogs you read or classes you take trying out what you learn will surpass in learning experiences. Written forms can only tell you so much, so please try what you learn and always change things around and try out ideas. I find it enjoyable to think of a new thing I want to see if I can do and just start coding it in and see where my ideas fall short at and how to make them work in the real world.
Happy coding!