In a previous brief article here I introduced a powerful “object oriented” feature in C# called “Inheritance”, simply put a “Super Class” or a more appropriate word “Base Class” can have its members used in another class (a derived class).
This tutorial will cover a better view on inheritance and how it can make develeping a program much more extensive!
Firstly make a new Console application called “InheritingProducts” and make a new class called “IProduct”.
1 using System;
2
3 namespace InheritingProducts
4 {
5 class IProduct
6 {
7 }
8 }
Now add the keyword “abstract” before “class”:
1 using System;
2
3 namespace InheritingProducts
4 {
5 abstract class IProduct
6 {
7 }
8 }
The keyword “abstract” means that this class can not be instantiated as an object i.e.
12 IProduct product = new IProduct();
This will throw a CS0144 error that states this class cannot be instantiated , instead an abstract class allows us to only “derive” from it, in other words this class is like a template for other classes to use.
Ammend the “IProduct” class to look like this:
5 abstract class IProduct
6 {
7 private string _Name;
8 private decimal _Cost;
9
10 public decimal Cost
11 {
12 get { return _Cost; }
13 set { _Cost = value; }
14 }
15
16 public string Name
17 {
18 get { return _Name; }
19 }
20
21 public IProduct()
22 {
23 _Name = GetType().Name;
24 _Cost = 0.0m;
25 }
26
27
28 }
Now other classes can inherit members from this class, when this happens we say “The derived class is a” IProduct.
Before we start deriving classes have a look at this:

Because each derived class (“Computer”,Guitar” and “Bike”) have the members from “IProduct”, they are an instance of “IProduct”, IProduct being the “base” or “super” class.
When their is a group of similar object types such as a “Product”, inheritance/generalization should be used.
Add a new class called “Computer”:
6 namespace InheritingProducts
7 {
8 class Computer
9 {
10 }
11 }
Now add the following next to “Computer”
“:IProduct” – without quotes:
8 class Computer:IProduct
The “:Classname” means to inherit the class, what ever the class name is ; in this case “IProduct”.
Add a constructor to this class:
8 class Computer:IProduct
9 {
10 public Computer()
11 {
12 this.Cost = 499.99m;
13 }
14 }
You haven’t declared the variable “Cost” but no error is provided… This is because you have actually declared it, in the “IProduct” class which is now part of this class!
Go to “Program.cs” and implement an object of “Computer”:
5 class Program
6 {
7 static void Main()
8 {
9 Computer product = new Computer();
10
11 Console.WriteLine(“Product Name {0}”, product.Name);
12 Console.WriteLine(“Product Cost {0:c}”, product.Cost);
13 Console.ReadKey();
14 }
15 }
Debug the program and you should see the following:

We have successfully inherited, derived and implemented an inherited concept!
If we take a look at “Program.cs” source you will see two “Console.WriteLine()” methods being used to display details from this class, as a keen developer you should know that abstracting code is valuable and that this can be ridden of, make a new class called “ProductFunctionality” and add the following code to it:
8 static class ProductFunctionality
9 {
10 static private IProduct _Product;
11
12 static public void Display(IProduct product)
13 {
14
15 if (product != null)
16 {
17 _Product = product;
18
19 Console.WriteLine(“Product Name {0}”, product.Name);
20 Console.WriteLine(“Product Cost {0:c}”, product.Cost);
21 }
22 }
23 }
This class acts as an operator of every derived class from “IProduct”, what this means is that we will use this class to access methods in relation to a product for example, the “Display()” method will display the products details. The class has been made static as I felt there was no need to make an initalization for this class as its part of this programs engine.
Ammend “Program.cs” to this:
7 static void Main()
8 {
9 Computer computer = new Computer();
10 ProductFunctionality.Display(computer);
11 Console.ReadKey();
12 }
Thats one way if you prefer a more readable code (as so far our products dont have much to go on) try this :
7 static void Main()
8 {
9 ProductFunctionality.Display(new Computer());
10 Console.ReadKey();
11 }
The problem with this version is that you cant manipulate the “Computer()” instantiation as its out of scope within “Display()”.
Add Two other class one called “Guitar” and another called “Bike” and derive from IProduct (like we did with “Computer.cs”), give them a unique price as well.

Click to enlarge
As you may see, its now very easy to add more implementation of a product !
I hope you enjoyed this tutorial!
GD Star Rating
loading...
GD Star Rating
loading...