Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:
 
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 11-Feb-13 9:33am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
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:
Console.WriteLine(classHeader);
to
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 Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
public override void DisplayInfo()
{
    Console.WriteLine("DisplayInfo() called from the derived class");
    base.DisplayInfo();
}
Just play with these things, and look at the results.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100