Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

Please i want to make sure that i understand those concepts well so please tell me if i am right :

-We declare a virtual method in a class so we can write a code in it and override it in a child class or hiding it by new keyword.

-We declare an abstract method in a class and we cant write a code in it but we can override it in a child class or hiding it by new keyword.

-A question goes here : cant i override a function without declaring it as virtual and abstract in the parent class ?

Thank you.
Posted
Comments
Sergey Alexandrovich Kryukov 1-Feb-12 13:23pm    
It looks like the questions are asked by a novice, but they are correct and make sense (and actually asked more for just reassurance), so I voted 5 for correctly posed question, which happens pretty rarely these days. (Most questions are not even questions.)
--SA
mhamad zarif 1-Feb-12 13:34pm    
thank you. I always try to search the internet and come out with results by myself but sometimes i need the help of experts to make sure that what i deduced was correct.
Sergey Alexandrovich Kryukov 1-Feb-12 14:18pm    
Good to know that. Getting to the essence of things is very important and will pay off very well later.

By the way, your question sparkled some discussion on delicate detail of syntax which is maybe not so principle and rarely used by good to know. Please see our little argument with Pete over his answer and my reply; please see further discussion and my update in my answer (commented [EDIT]).

So, thank you for raising the issue. :-)
--SA
mhamad zarif 1-Feb-12 14:25pm    
Those days i am so interested in oop, since i am moving to a big company for the first time in my life and they work on oop. So iam gaining knowledge in its principles(poymorphism,abstraction,encapsulation,inheritance),and i managed to understand everything, but i still have a problem in designing my classes reflecting my tables in database.So i think when i staart working i will gain this knowledge.
Sergey Alexandrovich Kryukov 1-Feb-12 15:33pm    
Well, Analysis is apparently easier than Synthesis.
Wish you success in both :-)
--SA

You write a virtual method to provide the ability to override it, not to hide it.

Abstract method - this time you must override it (actually, you're providing an implementation) - you don't hide it.

Strictly speaking, if the method exists in a non sealed class, you could changed the implementation using the new keyword. The problem with this, though, is that you could end up not setting something or changing something that further methods on that class rely on. This approach hides the method in the class being derived from, so you have no access to set it.

Remember, if a method wasn't declared as virtual, there was a reason for this.

What are you trying to achieve here?
 
Share this answer
 
Comments
mhamad zarif 1-Feb-12 12:01pm    
After further reading i deduced the following : Virtual method is a method that has implementation and it "can" be overridden in child class(not necessarily overridden).While abstract method has no implementation and it "must" be overridden in child class. Also abstract methods are only declared in abstract class while virtual can be declared in any class(abstract or not).And i cant override a method that is neither virtual nor abstract.Finally i use hiding when a method in parent class is neither abstract nor virtual in order to have different implementation in child class. Do you agree ?
Sergey Alexandrovich Kryukov 1-Feb-12 13:16pm    
Mhamad,

I think you need to understand the mechanism itself using the "gray box" approach. To do that, you need to read about virtual method tables, "dynamic dispatch" and "late binding". You can start with the Wikipedia articles you can easily find on these topics.

--SA
Pete O'Hanlon 1-Feb-12 12:15pm    
Correct.
mhamad zarif 1-Feb-12 12:45pm    
thanks :)
Sergey Alexandrovich Kryukov 1-Feb-12 13:14pm    
Pete, I added my answer.

In your answer, "you don't hide" is not 100% accurate. You can actually use hiding with "virtual", I tried it, with reasonable results. Completely different aspects are messed together though, so it's best avoided. It's kind of hard to describe all possible variant despite the fact it is all trivial, but I tried. Please see my answer.

Also, it looks like you did not answer the last question (the answer is: "no, you cannot").

I'll vote 4 this time.
--SA
First of all, there is no such thing as child class. The relationship can be "base class (ancestor) — derived class"). It is also called "generalization".

The keyword "new" is important to avoid compilation warning related to the fact that you hide some member of the base class. Consider this:

C#
class Base {
    internal virtual void Shaded() {}
}
class Derived : Base {
    internal new void Shaded() { }
}


[EDIT: to prove my point in a little argument with Pete, please see his answer]

Exact same thing is valid if the method in a base class is abstract:
C#
abstract class Base {
    internal abstract void Shaded();
}
abstract class Derived : Base {
    internal new void Shaded() {}
}


[END EDIT]

If you remove new, the warning is: "Warning 2 'Derived.Shaded()' hides inherited member 'Base.Shaded()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword."

By saying new you say: "Yes, I hide the member, but not accidentally. I know what I'm doing. Don't worry, comrade compiler, I'll be just fine without your silly warning!"

Needless to say, it should be best avoided in most cases (which do exist). It's even better not to mess up with virtual (as shown above), but it principle this is possible.

Now, if you remove virtual from Base.Shaded or add virtual to Derived.Shaded, the effect will be the same. In these cases, nothing object-oriented happens. The only object-oriented effect (late binding) is possible when first method is marked virtual or abstract, and the second one — override. Then the dynamic dispatch of the call happens even if you compile-time type is Base.

That comes to your last question. The answer is: no.

—SA
 
Share this answer
 
v6
Comments
mhamad zarif 1-Feb-12 13:14pm    
Thank you very much. You made it crystal clear as Pete O'Hanlon did(look up please). I always learn from you SAKryukov. You are just and simply great !
Sergey Alexandrovich Kryukov 1-Feb-12 13:17pm    
You are simply a shameless flatterer :-) but thank you very much for your nice words.
You are very welcome.

Good luck, call again.
--SA
mhamad zarif 1-Feb-12 13:20pm    
why are you saying to me shameless flatterer ?
Sergey Alexandrovich Kryukov 1-Feb-12 13:32pm    
"Shameless flatterer" is the cliche of English language. Sorry if I confused you with this thing.

It means: "How can you attribute so high qualities to me which I don't really have?" or "You are telling so exaggerated compliments to me which I don't really deserve, but you are telling them feeling no shame :-)"

"To flatter" means to tell overly nice or exaggerated compliments to someone.

--SA
mhamad zarif 1-Feb-12 13:34pm    
ah OK. My bad i did not know that it means this. But at the end off course you deserved what i said with no doubt.

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