Darren Young
Darren Young

Reputation: 11100

c# inheritance help

I am fairly new to inheritance and wanted to ask something. I have a base class that has lots of functionality that is shared by a number of derived classes.

The only difference for each derived class is a single method called Name. The functionality is the same for each derived class, but there is a need for the Name distinction.

I have a property in the base class called Name. How do I arrange it so that the derived classes can each override the base class property?

Thanks.

Upvotes: 4

Views: 125

Answers (7)

BrunoLM
BrunoLM

Reputation: 100381

Declare your method as virtual

public class A
{
    public virtual string Name(string name)
    {
        return name;
    }
}
public class B : A
{
    public override string Name(string name)
    {
        return base.Name(name); // calling A's method
    }
}
public class C : A
{
    public override string Name(string name)
    {
        return "1+1";
    }
}

Upvotes: 4

Jackson Pope
Jackson Pope

Reputation: 14660

In the base class:

public virtual string Name { get; set; }

In the derived classes:

public override string Name { get; set; }

However, if the only difference between the classes is that they have different names, I'd argue that instead of inheritance you should just use the base class with the Name set in the constructor:

e.g.

public class MyObject
{
     public string Name { get; private set; }

     public enum ObjectType { TypeA, TypeB, ... }

     public MyObject(ObjectType obType)
     {
          switch (obType)
          {
              case ObjectType.TypeA:
                  Name = "Type A";
              // and so on
          }
     }
} 

Upvotes: 2

Ian Griffiths
Ian Griffiths

Reputation: 14567

Well I'm not sure from your description that inheritance is actually the right solution to the problem but here's how you make it possible for a property to be overridden:

class Base
{
    public virtual string Name { get; set; }
}

But do you need it to be writable? A readonly property may make more sense in which case this might work:

class Base
{
    public virtual string Name
    {
        get { return "BaseName"; }
    }
}

class Derived : Base
{
    public override string Name
    {
        get { return "Derived"; }
    }
}

Upvotes: 2

Brosto
Brosto

Reputation: 4565

You can declare it with a virtual or abstract keyword in the base class, then the derived class can over-ride it

Upvotes: 3

JaredPar
JaredPar

Reputation: 755537

To enable each derived class to override the property you just need to mark the property as virtual

class Base {
  public virtual Property1 { 
    get { ... }
    set { ... }
  }
}

Upvotes: 2

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263147

Use a virtual property:

class Base
{
    public virtual string Foo
    {
        get;
        set;
    }
}

class Derived : Base
{
    public override string Foo
    {
        get {
            // Return something else...
        }
        set {
            // Do something else...
        }
    }
}

Upvotes: 3

mcabral
mcabral

Reputation: 3558

you need to declare your property (in the base clase) as virtual

Upvotes: 2

Related Questions