Click here to Skip to main content
15,883,750 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
Hi Guys,

In lot of interview I am facing a small confusion.
I am having two classes ChildClass and Base Class
C#
class BaseClass
    {
        public virtual void Test()
        {
            Console.WriteLine("BaseClass:Test()");
        }
    }
    class ChildClass : BaseClass
    {
        public override void Test()
        {
            Console.WriteLine("ChildClass:Test()");
        }
    }


Now My Questions is what is the use of virtual classes
MIDL
ChildClass B = new ChildClass();
B.Test();
BaseClass C = new ChildClass();
C.Test();


I can access Test() function with in heritence and without inheritence also na... Then what is the use of Inheritence.

Please clarify me.

Warm Regards,
Ali
Posted
Updated 21-Mar-17 4:43am

you can extend some base functionality in a subclass.

http://www.csharp-station.com/Tutorials/lesson08.aspx[^]

good luck!
 
Share this answer
 
The trick is that you can use a reference to the base class to reference an inherited one and, at runtime, when you call a virtual function using that reference, the overridden one in the inherited class is called. Consider the code below:

C#
class BaseClass
{
   public virtual void Test()
   {
      Console.WriteLine("BaseClass:Test()");
   }
}

class ChildClassA : BaseClass
{
   public override void Test()
   {
      Console.WriteLine("ChildClassA:Test()");
   }
}

class ChildClassB : BaseClass
{
   public override void Test()
   {
      Console.WriteLine("ChildClassB:Test()");
   }
}


Now suppose that you write something like this:

C#
BaseClass obj = new ChildClassA();
obj.Test();
obj = ChildClassB();
obj.Test()


The output that you get is something like this:

ChildClassA:Test()
ChildClassB:Test()
 
Share this answer
 
Comments
Alim12345 5-Aug-10 0:36am    
Reason for my vote of 4
Ok...
Alim12345 5-Aug-10 0:38am    
Hi Sauro, Thanks for your reply. I need one more clarification. As U said
BaseClass obj = new ChildClassA();
if I want to access BaseClass.Test() function how can I access this obj?
Please reply me.
Regards,
Alim
Sauro Viti 5-Aug-10 1:27am    
To access the base class method you should write:
BaseClass obj = new BaseClass();
obj.Test();
If you mean "how to access the BaseClass version of the method on a child class" the answer is:
1) from inside the child class use base.Test();
2) from outside the child class is not possible
AminMhmdi 15-Jun-17 13:29pm    
tnx for explain but what is benefit that we have one reference for multi derived class?? is this trick for Save memory or something??
There is no such thing as a virtual class.
Surely you must confuse classes with methods.
Please read some very basic documentation about object orientated programming.
 
Share this answer
 
My dear Friend,
Inheritance is the sole feature of Polymorphism by creating base class child class relationship.

by using this relationship by creating base class instances which can references the child class members which are inherited only.

So,
C#
class base
    {
        public virtual int sum(int a,int b)
        {  return (a+b);}
    }

    class child
    {
        public override int sum(int a, int b)
        {  return (a+b)/10 ;   }
    }
    class Main
    {
      base b =new child();
      b.sum(10,10);
           //this gives ans 2 since it calls child class inherited method
           // this is only possible through inheritance and it code reusability                    
    }


If u are satisfied with this ans then give feed back.
 
Share this answer
 
v2
Comments
[no name] 7-Jul-12 10:10am    
Don't you think after 2 years his interview is pretty much over with?
 
Share this answer
 
Comments
[no name] 8-Jul-12 6:12am    
Don't you think after 2 years his interview is pretty much over with?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900