1 Replies - 343 Views - Last Post: 12 October 2012 - 10:26 PM Rate Topic: -----

#1 Natman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-September 12

Multi-Layered Inheritance Question

Posted 12 October 2012 - 10:08 PM

So I have a complex inheritance tree in the project I'm working on. I have a base class with a virtual method that will be overridden by every inheriting class down the line, like so:

class baseClass
{
public virtual void method()
{
}
}

class class1 : baseClass
{
public override void method()
{
}
}

class class2: class1
{
public override void method()
{
}
}



Is this the correct way to do this? When I declare all the subsequent methods as virtual I get a warning because it hides the inherited method of the parent class, however that method still gives me the functionality I want.

Is This A Good Question/Topic? 0
  • +

Replies To: Multi-Layered Inheritance Question

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Multi-Layered Inheritance Question

Posted 12 October 2012 - 10:26 PM

Quote

When I declare all the subsequent methods as virtual

That's not shown in your code.

And it doesn't really make sense. The base class' method is already virtual. But two generations down you want to override the first generation's inherited form of the method, which was already an override of the base class' method. So in what way to you imagine that overriding the first generation's virtual method would be different than overriding the base class' method?

One thing it might do is stop the chain of inheritance.
If class one is virtual, and class two inherits from class one, and class three from class two - it would be impossible for class three to ever call a method in the true base class, because the virtual in class one is hiding it.

You might want to reconsider design. If every inherited class is going to override it anyway, this might be a better place to implement an interface instead of inheritance.


This might help visualize inheritance:

Think of objects in coding just as you would objects in the real world.

A Dodge Ram is an object.
It is made up of smaller objects: Engine, doors, tires
Each of those is made up of smaller objects: Bolts, pistons, etc.

Objects in coding can inherit from each other, usually from the general to the specific.

  • Class vehicle
    • Class truck : vehicle
      • Class Ram : Truck
        • class 2500FWD : Ram


A class is the blueprint for instanciating (making an instance of) the object.

DodgeRam is a class describing how to make an instance, but itself is not an actual thing you can interact with.

myDodgeRam is an instance of the class DodgeRam

I can do things with the object instance myDodgeRam

myDodgeRam.SerialNumber = 123456789;
myDodgeRam.FillUpTank();
float fuelLevel = myDodgeRam.FuelTankPercentageFull;
if (myDodgeRam.IsReady) myDodgeRam.StartEngine();


Anything defined in the base class is available to a child.

class truck : vehicle
{

    public float FuelTankPercentageFull
    {
       get; set;
    }
}

class DodgeRam : truck
{
    // I don't have to define a FuelTankPercentageFull here because I inherit it from my parent
}


Methods defined as virtual in the base class can be overridden by the child class (at least in .NET languages like C#). This is often to account for more specific needs.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Do something to start the engine
       return true; // No checks or requirements
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return true;
        }
        return false;
    }
}


You can even have a child class call the base classes methods which is often the smart way to go.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Notice there are no safety checks before trying to start up.
       try
       {
          // Do something to start the engine
          return true; // because we succeeded
       }
       catch(exception error)
       {
           return false; // because there was an error
       }
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return base.StartEngine();
            // Now all the electrical work is in the base class
            // and not repeated in every child.
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1