Method hiding in c# with a valid example .why is it implemented in the framework.? what is the Real world advantage.?

C# not only supports method overriding, but also method hiding. Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method has to be declared using the new keyword.

The correct class definition in the second listing is thus: using System; namespace Polymorphism { class A { public void Foo() { Console. WriteLine("A::Foo()"); } } class B : A { public new void Foo() { Console. WriteLine("B::Foo()"); } } class Test { static void Main(string args) { A a; B b; a = new A(); be = new B(); a.Foo(); // output --> "A::Foo()" b.Foo(); // output --> "B::Foo()" a = new B(); a.Foo(); // output --> "A::Foo()" } } }.

I guess the same can be acheived without the new keyword, only thing is the compiler gives a warning . Otherwise does it have any significant difference.? – vijaysylvester Jul 29 '09 at 4:02 no, there is no difference, compiler gives a warning and it works as you wrote new keyword if you don't pay attention to warning – ArsenMkrt Jul 29 '09 at 4:21 The important thing here is that the type of the reference determines the method called. – Anderson Imes Sep 14 '09 at 7:35 Complete article can be found at akadia.

Com/services/dotnet_polymorphism. Html – Groo Dec 31 '09 at 8:21.

I think ArsenMkrt's example is not fully correct, at least it does not fully explain the hiding feature. By dropping the new keyword from the Foo method in class B, you will still get the output A::Foo() B::Foo() A::Foo() In a programming language like Java, where all methods are "virtual", you'd expect to get the output A::Foo() B::Foo() B::Foo() by taking ArsenMkrt's code above, due to the instantiation A a; B b; a = new A(); be = new B(); a.Foo(); b.Foo(); a = new B(); //WriteLine("A::Foo()"); } } class B : A { public override void Foo() { Console. WriteLine("B::Foo()"); } } Now is where the "new" keyword comes in.

Actually when you leave the "override" from B::Foo(), then you again would hide A::Foo() meaning that you don't override it's default behavior and you don't achieve polymorphism, i.e. You get "A::Foo()" again as output. The same can be achieved - and here's where I don't 100% understand why you HAVE to put it - by placing the "new" keyword like.. class A { public virtual void Foo() { Console.

WriteLine("A::Foo()"); } } class B : A { public new void Foo() { Console. WriteLine("B::Foo()"); } } and you again get the output A::Foo() B::Foo() A::Foo().

Question is market C# as you see – ArsenMkrt Jul 28 '09 at 13:31 3 Juri highlights a difference with Java, but he is talking about C#... – jeroenh Jul 28 '09 at 13:39.

One use I sometimes have for the new keyword is for 'poor mans property covariance' in a parallell inheritance tree. Consider this example: public interface IDependency { } public interface ConcreteDependency1 : IDependency { } public class Base { protected Base(IDependency dependency) { MyDependency = dependency; } protected IDependency MyDependency {get; private set;} } public class Derived1 : Base // Derived1 depends on ConcreteDependency1 { public Derived1(ConcreteDependency1 dependency) : base(dependency) {} // the new keyword allows to define a property in the derived class // that casts the base type to the correct concrete type private new ConcreteDependency1 MyDependency {get {return (ConcreteDependency1)base. MyDependency;}} } The inheritance tree Derived1 : Base has a 'parallell dependency' on ConcreteDependency1 : IDependency'.

In the derived class, I know that MyDependency is of type ConcreteDependency1, therefore I can hide the property getter from the base class using the new keyword. EDIT: see also this blog post by Eric Lippert for a good explanation of the new keyword.

1 interesting -> +1 – Juri Jul 28 '09 at 13:14 That was really good dude . I was not aware of those intricacies – vijaysylvester Aug 22 '09 at 14:27 Agreed, +1 for informative and useful example. – shambulator Jun 16 '10 at 9:43.

The new declaration may be any type of enity. It (new) specifies that the class hide an inherited member by the same name. A new modifier does not remove the inherited type members but that name unavailable in the dereived class.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions