Override methods should call base method?

I’m just running NDepend against some code that I have written and one of the warnings is Overrides of Method() should call base.Method().

The places this occurs are where I have a base class which has virtual properties and methods with default behaviour but which can be overridden by a class which inherits from the base class and doesn’t call the overridden method.

For example, in the base class I have a property defined like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>protected virtual char CloseQuote
{
get
{
return '"';
}
}
</code>
<code>protected virtual char CloseQuote { get { return '"'; } } </code>
protected virtual char CloseQuote
{
    get
    {
        return '"';
    }
}

And then in an inheriting class which uses a different close quote:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>protected override char CloseQuote
{
get
{
return ']';
}
}
</code>
<code>protected override char CloseQuote { get { return ']'; } } </code>
protected override char CloseQuote
{
    get
    {
        return ']';
    }
}

Not all classes which inherit from the base class use different quote characters hence my initial design.

The alternatives I thought of were have get/set properties in the base class with the defaults set in the constructor:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>protected BaseClass()
{
this.CloseQuote = '"';
}
protected char CloseQuote { get; set; }
public InheritingClass()
{
this.CloseQuote = ']';
}
</code>
<code>protected BaseClass() { this.CloseQuote = '"'; } protected char CloseQuote { get; set; } public InheritingClass() { this.CloseQuote = ']'; } </code>
protected BaseClass()
{
    this.CloseQuote = '"';
}

protected char CloseQuote { get; set; }

public InheritingClass()
{
    this.CloseQuote = ']';
}

Or make the base class require the values as constructor args:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>protected BaseClass(char closeQuote, ...)
{
this.CloseQuote = '"';
}
protected char CloseQuote { get; private set; }
public InheritingClass()
base (closeQuote: ']', ...)
{
}
</code>
<code>protected BaseClass(char closeQuote, ...) { this.CloseQuote = '"'; } protected char CloseQuote { get; private set; } public InheritingClass() base (closeQuote: ']', ...) { } </code>
protected BaseClass(char closeQuote, ...)
{
    this.CloseQuote = '"';
}

protected char CloseQuote { get; private set; }

public InheritingClass()
    base (closeQuote: ']', ...)
{
}

Should I use virtual in a scenario where the base implementation may be replaced instead of extended or should I opt for one of the alternatives I thought of? If so, which would be preferable and why?

1

It is a valid design decision to have a default implementation of a method in a base-class that should be completely replaced when the method is overridden in a derived class.

Thus, you could regard the warnings from NDepend as a false-positive for your code. I would check the NDepend documentation to see if there is an option or source-code annotation that tells NDepend that not calling the base-class implementation is correct here to get rid of the clutter.

If there is no such option (or it does not work for properties), my preference would be the first alternative (overriding the value of CloseQuote in the derived-class’ constructor).

It’s a strange warning NDepend gives you. Surely not all overridden methods have a need to call the base method, so I think the text Method() should call base.Method() is too strong. It does make it look like you may be doing something wrong, which you clearly are not.

This has nothing to do with using virtual though. Was that supposed to be a different question?

2

The warning is trying to tell you that, since you’ve defined an is-a relationship with the super class, all protected and public behavior of the super class should be reachable from the subclass.

Most often the real reason you want to override a method to replace its behavior completely is a demand for composition. E.g. instead of overriding the method closeQuote, you might want to create an interface ‘QuoteDefinitionProvider’ (or such like) and feed it to the superclass.

Other reasons might be:

  • super class should be abstract, but isn’t, so a ‘default’ implementation is given. Solution: make super class abstract.
  • subclass implementation can be optimized for the special case. This seems to be a sound reason to completely override the method and not call super.

So, before ignoring this warning, check whether your situation demands composition instead of inheritance. In my professional experience, I’ve seldom found a true need for class inheritance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật