

If you want it to be visible in derived classes you need it to be "protected". "private" means that it's only visible to that class and nothing else, including derived classes. But at least this is explanation of what's wrong with your code.īecause it's private in the base class. It's unclear what you're trying to do and why you need casts at all (or even arrays or even any class besides the form here). It's in C# because C# copied Java without thinking on that point. In general, casting arrays is bad, don't do it.
#Upcast and downcast in java code#
If you ever put an actual Employee in that Employee array, the code will fail. You could do:Įmployee employee = manager ISalary someemployee = employee as Manager // get the first employee in the arrayĪnd incidentally, in this case, it won't fail at run-time, because you've actually only put Manager objects in that Employee array, and Manager implements ISalary. The compiler won't let you do that, and trying to work around the compiler error with a cast will just turn the compile-time problem into a run-time problem. ISalary someemployee = employee // get the first employee in the arrayĪnd that doesn't make sense either, because Employee doesn't implement ISalary.

You're trying to assign a collection of values to a single value, which makes no sense. Otherwise the name parameter is not recognized by the class.Įmployee employee = manager ISalary someemployee = employee as Manager Įmployee is an array. When downcasting the conversion returns to null.Īlso Under manager class i have to mention the lineĭespite using the base keyword in the constructor above it. ISalary someemployee = employee as Manager Ĭannot convert type 'WindowsFormsApplication26.Employee' to 'WindowsFormsApplication26.Manager' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion Private void button1_Click(object sender, EventArgs e) Public Manager(string name, int salary) : base(name) Here there are 2 classes Employee(base class) and Manager(sub class) Manager inherits from Employee.I have added 2 Interfaces IName and ISalary so that i can downcast.
