overriding implemented base class methods

I read somewhere that the chain of inheritance breaks when you alter a behavior from derived class. What does “altering a behavior” mean here? Is overriding an already implemented method in base class considered as “altering behavior”? Or, does the author mean altering method signatures and the output?

Also, I ready Duplicating code is not a good practice, and its a maintenance nightmare. Again, does overriding an already implemented method in base class considered “Duplicating code”? If not, what would be considered as “Duplicating code”? I

I read somewhere that the chain of inheritance breaks when you alter a behavior from derived class. What does “altering a behavior” mean here?

Using C# as an example, you could have a base class such as :

public class SomeBaseClass
{
    public virtual void DoSomething()
    {
        Console.WriteLine("Hello world!");
    }
}

And then a child class such as this:

public class MyChildClass : SomeBaseClass
{
    public override void DoSomething()
    {
        Console.WriteLine("This method now does something completely different!");
    }
}

However, if you added base.DoSomething(); to the method MyChildClass, it would call both the parent method (outputting “Hello world!”) and the new method (outputting “This method…”). The order they are displayed depends on where you put the base.DoSomething(); method call.

By avoiding calling the base method, I believe you would break the chain of inheritance, as it no longer performs the expected method of the parent class.

Another option is to use the new syntax on the method like so:

new public void DoSomething()
{
}

This is called method hiding and lets you again define completely new behavior, but exposes a different method to the outside world, than the one that is visible by SomeBaseClass. I haven’t had a need to use this myself, but I understand it can be helpful in some situations.

Of course, your milage may vary for these syntaxes and commands depending on the language of choice, but the general idea is the same.


I read that Duplicating code is not a good practice, and its a maintenance nightmare. Again, does overriding an already implemented method in base class considered “Duplicating code”?

Duplicating code is a bad practice, as it can lead to errors during maintenance. Suppose you had a function that calculates a value using some complex arithmetic or the like. Instead of properly creating a method for it, you just copy and paste it a dozen times as needed. Now, when something changes (and it will! Oh, things will change) you have to locate it a dozen times, and put the correct code in a dozen times. By putting it in a method you just change it once, and everything that uses that code is corrected as well.

Abstraction and inheritance reduce code duplication, because you can make a base class or method as a template, and you only have to write it once. Smart object design allows you to make highly-reuseable code which is proven to be tested and error-free. Plus, following inheritance guidelines will allow for child classes that can use code under the assumption that it “just works”, and it can override the implementation, providing new or more specific behavior for itself as needed.

1

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