Click here to Skip to main content
13,863,642 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

25.2K views
6 bookmarked
Posted 27 Jul 2013
Licenced CPOL

What is C# Fragile Class Problem?

, 27 Jul 2013
Rate this:
Please Sign up or sign in to vote.
This post describes the C# fragile class problem

Inheritance is a great power by which developers can increase reusability in parent-child relationship. But with great powers comes greater responsibility and also problems. One of the problems which creeps in is the “Fragile parent class problem”.

Take a simple scenario (below is the image) where we have a simple parent child class relationship. Assume that this child class is used in lot of projects and installed in lot of places. Now assume after the child class has been deployed across locations, after some months, there are some changes in the parent class.

These changes can have cascading and unexpected behavior in child class as well. This unexpected behavior on child classes is termed as “Fragile class problem”.

Consider the below scenario where we have a parent class called as “DbParent” with a virtual method “Insert”.

class DbParent
{    
        public virtual void Insert()
        { 
            Console.WriteLine("Parent insert");
        }
}  

Below is a simple child class with his own “Insert” implementation which is installed across various locations.

class DbChildClass : DbParent
{
       public void  Insert()
        {
            Console.WriteLine("Child inserts");
        }
} 

Now let’s say after some months of deployment parent class developers without understanding the impact on child classes go and add a new method “Add”. This “Add” method calls the “Insert” method internally (below is the code snippet for the same).

class DbParent
{
public  void Add() // Adds method with out consulting
{
      this.Insert();
}
public virtual void Insert()
{
            Console.WriteLine("Parent insert");
}
} 

Now client programs who invoke the “Add” method by creating child class objects expect that the “Child” class “Insert” implementation should be called.

DbChildClass o = new DbChildClass();
o.Add();
Console.Read();  

But whoaaaa, if you run the below code, you will see the parent class “Insert” is called which is not EXPECTED.

So the next question is what is the proper way of solving this issue? The first thing as per my understanding (I can be wrong) is that there are no standard and common ways of solving the problem.

One of the thoughts which goes here is, if the base class is very fragile (will change time to time) then inheritance should not be allowed further. So by marking the classes as “sealed” or the fragile method as “sealed”, this problem can be controlled.

The second way would be to avoid calling virtual methods in the parent class in other methods.

I am myself hunting for proper standard solutions so if you have some better suggestions, please do write down the same in the comments below.

Also, I would like to point out that .NET Framework handles fragile parent class issues to a certain extent by itself. For instance, in the below code, even though the child class method “HandleNumerics” has been passed an integer value, it is still invoking the “HandleNumerics” with double data type.

So the framework assumes that the child class method should be given preference rather than parent class.

class Parent
    { 
        public virtual void HandleNumerics(int d)
        {
            Console.WriteLine("Parent numeric ");
        }
    } 
    class child : Parent
    {
        public  void  HandleNumerics(double d)
        {
            Console.WriteLine("Child numeric");
        }        
    }
    class Client
    {
        public static void Main()
        {
            child o = new child();
            int i = 100;
            o.HandleNumerics(i);
        }
    }   

The above code will display “Child Numeric” rather than displaying “Parent Numeric”.

Below is a simple youtube video which demonstrates the above auto fragile handling methodology by .NET framework. Many C# developers think that this is a defect but it’s actually done “By DESIGN” to solve the “Fragile base class” problem.

License

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

Share

About the Author


You may also be interested in...

Pro

Comments and Discussions

 
QuestionInheritance in C# doesn't work like described in this Pin
Member 1271418931-Aug-16 19:46
memberMember 1271418931-Aug-16 19:46 
Questionmarking the classes as “sealed” or the fragile method as “sealed” this problem can be controlled. Pin
lalli17297-Dec-15 18:58
memberlalli17297-Dec-15 18:58 
QuestionFragile classes: symtom not the root cause Pin
Wolfgang Stelzhammer23-Sep-14 5:37
memberWolfgang Stelzhammer23-Sep-14 5:37 
GeneralMy vote of 1 Pin
albert arul prakash1-Aug-13 21:51
memberalbert arul prakash1-Aug-13 21:51 
GeneralMy vote of 1 Pin
syqosis28-Jul-13 15:58
membersyqosis28-Jul-13 15:58 
BugImproper overriding implementation Pin
Juraj Maciak27-Jul-13 9:40
memberJuraj Maciak27-Jul-13 9:40 
GeneralRe: Improper overriding implementation Pin
Shivprasad koirala27-Jul-13 9:50
memberShivprasad koirala27-Jul-13 9:50 
GeneralRe: Improper overriding implementation Pin
albert arul prakash1-Aug-13 21:48
memberalbert arul prakash1-Aug-13 21:48 
GeneralRe: Improper overriding implementation Pin
Shivprasad koirala2-Aug-13 16:24
memberShivprasad koirala2-Aug-13 16:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 27 Jul 2013
Article Copyright 2013 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid