Hi guys,
I have a class like below. My queries are highlited below in Main method.
class Base
{
internal void DoWork()
{
Console.WriteLine("From Base");
}
}
class Derived: Base
{
internal void DoWork()
{
Console.WriteLine ("From Derived");
}
internal void DoMoreWork()
{
Console.WriteLine("New Derived");
}
}
{
internal void DoWork()
{
Console.WriteLine("From Base");
}
}
class Derived: Base
{
internal void DoWork()
{
Console.WriteLine ("From Derived");
}
internal void DoMoreWork()
{
Console.WriteLine("New Derived");
}
}
class Program
{
static void Main(string[] args)
{
Derived d1 = new Base(); //Doesn't work why?
Base b1 = new Derived(); //Works why?
b1.DoWork(); //Execute Base method why?
{
static void Main(string[] args)
{
Derived d1 = new Base(); //Doesn't work why?
Base b1 = new Derived(); //Works why?
b1.DoWork(); //Execute Base method why?
}
Appreciate your responses for my highlited questions above.
Regards..

Pankaj Kumar ChoudharyPosted Mar 12, 2015, 4:16 AM
It is a concept of Inheritance in OOPS.OOps mainly use two methodology for Inheritance
1.Upcasting 2. Downcasting
Upcasting:
In other word we can say that.
An object of a derived class is a kind of the base class. Therefore the conversion from a derived class pointer to a base class pointer is perfectly safe, and happens all the time.
DownCasting:
The opposite process, converting a base-class pointer (reference) to a derived-class pointer (reference) is called downcasting. Downcasting is not allowed without an explicit type cast. The reason for this restriction is that the is-a relationship is not, in most of the cases, symmetric. A derived class could add new data members, and the class member functions that used these data members wouldn't apply to the base class
we can understand it such that.
Base b1 = new Derived();
is work correctly because compiler implicitly convert the derived class into base class and during run time complier treat derived class as a Base class.
Derived d1 = new Base();
compiler does not implicitly convert the base class object to drived class object.Because
complier use a "Is-a " relationship which always assume that new class is type of base class. It never assume that base class is a type of new class .So it will throw an error.
But we can conert the base class object to derived class object such that:
Base b1=new Base();
Derived d2 = b1 as Derived;
//It will work correct
Now your last question is that
b1.DoWork(); //Execute Base method why?
Because OOPS follow a hierarchy in which it call fun of base class first after that derived class. and at run time it doesn't chech that what type of address is contain by base class Obj.so it always call base class method.
If you want to correct your code then add "Virtual" keyword before method of base class
and add "override" before method of drived class.
like as:
class Base
{
virtual public void DoWork()
{
Console.WriteLine("From Base");
}
}
class Derived : Base
{
public override void DoWork()
{
Console.WriteLine("From Derived");
}
public void DoMoreWork()
{
Console.WriteLine("New Derived");
}
}
then it will work correct..
I give my best how much i know ...
sorry for english
Prakash TripathiPosted Mar 13, 2015, 1:47 AM