Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
we have a class abc which has method add().......
inherit it class xyz have same method add()...
so if we make a object of base class then this object call whic method...
base or drived class method
Posted

Don't worry about the others.

You wrote "if we make a object of base class then this object call whic method"

It will always call the base class method. The base class doesn't know about the inherited class. It doesn't matter if the function is declared virtual or not. It's not until you create an instance of the inherited class you have to worry about this.
 
Share this answer
 
It depends on if it is virtual or not. With this level of understanding (and very bad writing; this is simply impolite) you're not ready to ask questions here. First, read on OOP (http://en.wikipedia.org/wiki/Object-oriented_programming[^]), learn C++ and/or C# by manuals and exercises.

If you did, it would not be possible that you asked such questions.

Also, here are some learning guidance for your information: I have a problem with my program. Please help![^], locate and read my answer.

—SA
 
Share this answer
 
Comments
Sandeep Mewara 1-Mar-11 2:35am    
I did not noticed you had answered. Probably we both were trying at same time! :)
Sergey Alexandrovich Kryukov 1-Mar-11 2:46am    
Yes, probably, as usual...
Sandeep Mewara 1-Mar-11 3:12am    
Oops, I missed. 5 for your answer and help. :)
Sergey Alexandrovich Kryukov 1-Mar-11 4:12am    
Thank you, Sandeep.
--SA
we have a class abc which has method add().......
inherit it class xyz have same method add()...
so if we make a object of base class then this object call whic method...
base or drived class method


No one will answer that. It's simple. Will take hardly 5 minutes. Just try, do it and check it. Please don't expect a complete spoon-feed! :doh:
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Mar-11 2:47am    
Pretty good, 5,
--SA
If you're using C#:
Calling xyz.Add() where xyz:abc will call the Add() method of xyz class. xyz.Add() should be marked new or override depending on how abc.Add() is declared (virtual, abstract or none of them). In the method body you can call abc.Add() with base.Add().
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Mar-11 2:50am    
Not too bad, my 5.
If you decided to answer seriously (I'm afraid, OP would not understand anyway), you should have explained "new", many have no idea (most would not know there is such thing); it's about warning, so... Actually, this is about virtual mechanism; is someone didn't get it, answering such Questions is pointless...
--SA
Dima Popov 1-Mar-11 3:14am    
Thanks! I don't mind answering questions like this. We all know that it's useless at some point but eh.. why not? :)
class ABC
{
Public void Add()
{
Console.Write("Base");
}

}
class XYZ:ABC
{
Public void Add()
{
Console.Write("Derived");
}

}

class Program
{
static void Main(string[]args)
{
ABC obj=new ABC();
obj.Add();

}
}

Answer:Base
 
Share this answer
 

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