Click here to Skip to main content
15,667,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am just beginning to learn C# and I'm trying to wrap my head around inheritence.
I'm also doing my best not to repeat code in order to make it easy to maintain so if I need to update a function then I only need to do so in one place.

Now to my problem.

I've been playing around with inheritence to achieve the items mentioned earler whilst also keeping classes self containted.
Just like to you use "ToString()" in most places I'd like to have a "DisplayInfo()" function but again, only defined once.

I've tried the following:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritenceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Hardware MyHardware = new Hardware();
            MyHardware.DisplayInfo();

            BIOS MyBIOS = new BIOS();
            MyBIOS.DisplayInfo();

            Console.ReadKey();
        }
    }

    class Hardware
    {
        public string classHeader;

        public Hardware()
        {
            classHeader = "This is the Hardware class";
        }

        public void DisplayInfo()
        {
            Console.WriteLine(classHeader);
        }
    }

    class BIOS : Hardware
    {
        public string classHeader;

        public BIOS()
        {
            classHeader = "This is the BIOS class";
        }
    }
}


The problem is that the function defined in the base class will only use the property "classHeader" in the base class.
I thought that the "classHeader" property in the subclass would override the base class but that does not seem to happen.

So how can I achieve:

- defining the function only once
- make the function available for all instances
- make the function display the properties for the current instance/class

Again, I'm a novice in this arena so bare with me if I'm not using the correct terms.

Regards
Richard
Posted

Quote:
- defining the function only once
- make the function available for all instances
You may actually achieve that with inheritance.


Quote:
- make the function display the properties for the current instance/class
This would break your very first requirement.


With inheritance you can write incremental code: define common behaviour in the base class and extend such a behaviour in the derived ones.
 
Share this answer
 
If you compile this you get a warning saying that "'BIOS.classHeader' hides inherited member 'Hardware.classHeader'. Use the new keyword if hiding was intended. If you use the new it will function correctly but not use a single method. " which gives you a clue.

If you remove the following line from the subclass BIOS your code functions as you'd expect:
public string classHeader;


In your original code (with the above declaration in place) the classHeaderPropety in BIOS hides the underlying classHeader in the Hardware class. As the DisplayInfo method in your code is defined in the Hardware class it uses the classHeader from the base Hardware not the one hiding it in BIOS which the method is "unaware" of, giving you your results.


To help explain (not for real use!), try changing your original BIOS class so it has a secondary method that writes out the display directly, nit via the superclass, e.g. to this:

C#
class BIOS : Hardware
{
    public string classHeader;

    public BIOS()
    {
        classHeader = "This is the BIOS class";
    }

    public void DisplayInfoBIOS()
    {
        Console.WriteLine(classHeader);
    }
}



and adding this line to your main function before the readkey:

MyBIOS.DisplayInfoBIOS();



As the new DisplayInfoBIOS is defined in the BIOS class, it uses the BIOS's version of classHeader, and you get the BIOS text.

As as final exercise to convince yourself, try changing the new DisplayInfoBIOS from:
C#
Console.WriteLine(classHeader);
to
C#
Console.WriteLine(base.classHeader);
and try and predict the result.


Obviously the thing to do is remove the line I mentioned above so both super and sub classes access the same member rather than hacking an new method in :)
 
Share this answer
 
The variable classHeader is to be used by the Hardware class and classes inheriting from it only. Hence it should not be public, but protected. protected is similar to private, but allows for derived classes to access that variable/property/function of the base class.
In the derived class, remove the declaration of the variable - you can now access the variable of the base class.
And another trick: you might want to change the behavior of a property/function in a derived class. Then it is advisable to add the virtual keyword in the base class, e.g. public virtual void DisplayInfo(). In the derived class, you can change it with the override keyword, e.g.
C#
public override void DisplayInfo()
{
    Console.WriteLine("DisplayInfo() called from the derived class");
    base.DisplayInfo();
}

Just play with these things, and look at the results.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900