When you create an Employee object, its Labour field 'l' is initialized to a new Labour object.
The Labour constructor therefore runs. However, before it does so, it first calls the base class's parameterless constructor.
As the base class is Employee, this causes 'l' to be initialized again to a new (and independent) Labour object.
So the Labour constructor runs again, the Employee constructor again gets called and the whole process repeats until eventually the stack overflows and the application terminates with an exception.
Although you haven't posted the code for the Manager, VicePresident and SalesManager classes, I imagine they'll form some sort of inheritance hierarchy and, if they ever got the chance, initializing the fields: 'm', 'vp' and 'sm' would all cause the stack to overflow for similar reasons.
You should be able to see that what I've said above is in fact happening by commenting out some lines from the Employee class:
class Employee
{
static double totalsalary,laboursalary1,managersalary1,vpsalary1;
//Labour l = new Labour("ABC", 9, 100);// <---------------------------Exception thrown here as stackoverflowexception
//Manager m = new Manager("PQR", 10000, 5000, 1000);
//VicePresident vp = new VicePresident("XYZ", 20000, 10000, 5000);
//SalesManager sm = new SalesManager("KLM", 8000, 2000, 500, 50);
public Employee()
{
//laboursalary1 = l.getTotalSalary();
//managersalary1 = m.getTotalSalary();
//vpsalary1 = vp.getTotalSalary();
}
public static double calculateTotalSalary(Labour l, Manager m, VicePresident vp, SalesManager sm)
{
totalsalary = laboursalary1 + managersalary1 + vpsalary1;
Console.WriteLine(totalsalary);
return totalsalary;
}
}
I'd expect that the application will then run and print "Employee" to the console.
So what you don't want here is to have fields of a derived class type being defined within its base class.
You'll therefore need to rethink the design of your Employee class to get the program to work properly.