简体   繁体   中英

Why we need to use new keyword if hiding is intended, when we can directly create ClassA object and call printA method of classA

Why we need to use new if hiding is intended, when we can directly create ClassA object and call printA method of classA

I'm confused why do we need this new keyword here when we have the option to directly create class object and call the required method:

                class Program
                    {
                        static void Main()
                        {
                            classA clsA = new ClassB();
                            clsA.printA();
                        }
                    }

                    public class classA
                    {
                        public classA()
                        {
                            Console.WriteLine("ClassA constructor");
                        }

                        public void printA()
                        {
                            Console.WriteLine("ClassA-PrintA Method");
                        }
                    }

                    class ClassB : classA
                    {
                        public ClassB()
                        {
                            Console.WriteLine("ClassB constructor");
                        }

                        public void printB()
                        {
                            Console.WriteLine("ClassB-PrintB Method");
                        }

                        public new void printA()
                        {
                            Console.WriteLine("Class B extending ClassA.PrintA Method");
                        }       
                  } 

Difference between below code implementation when anyway we have to call PrintA method of class A in both the cases...

1) Using new keyword with the method in the both the base and derived class ------------------------------------------------------------------

    class Program
                {
                    static void Main()
                    {
                        classA clsA = new classA();
                        clsA.printA();
                    }
                }

                public class classA
                {
                    public classA()
                    {
                        Console.WriteLine("ClassA constructor");
                    }

                    public virtual void printA()
                    {
                        Console.WriteLine("ClassA-PrintA Method");
                    }
                }

                class ClassB : classA
                {
                    public ClassB()
                    {
                        Console.WriteLine("ClassB constructor");
                    }
                    public new void printA()
                    {
                        Console.WriteLine("Class B extending ClassA.PrintA Method");
                    }
                }

2) Without using new keyword and no printA implementation in ClassB ----------------------------------------------------------------
class Program { static void Main() { classA clsA = new classA(); clsA.printA(); } }

                public class classA
                {
                    public classA()
                    {
                        Console.WriteLine("ClassA constructor");
                    }

                    public void printA()
                    {
                        Console.WriteLine("ClassA-PrintA Method");
                    }
                }

                class ClassB : classA
                {
                    public ClassB()
                    {
                        Console.WriteLine("ClassB constructor");
                    }
                }

what is the real time need of new keyword.

new keyword requirement is there to draw your attention to the fact that the method in the parent class will be hidden and not overridden.

Hiding isn't really one of the best practices when it comes to OOP, virtual methods are more common. IMHO the developers of the c# compiler decided that if the class declares a member with the signature that is equivalent to that of any other member's down the inheritance chain, and there is no override prefix - it might be a mistake, so the compiler asks whether hiding is intentional.

You don't need the new keyword unless you wish to hide the base class member, and then you do need the keyword as that is how the C# language is designed. The code would not compile without it.

Typically, you declare a function in your base class and not redefine in the derived class, or you define the base class function virtual (can be overridden) or abstract (must be overriden).

So: ClassB could be written without any printA function at all as it is already declared in the base class and is not abstract :

class ClassB : classA
{
    public ClassB()
    {
        Console.WriteLine("ClassB constructor");
    }

    public void printB()
    {
        Console.WriteLine("ClassB-PrintB Method");
    }
}

The above will compile and printA will be callable from ClassB but will use the implementation in ClassA .

Furthermore, consider this:

        classA clsA = new ClassB();
        clsA.printA();
        new ClassB().printA();

Output:

ClassA constructor

ClassB constructor

ClassA-PrintA Method

ClassA constructor

ClassB constructor

Class B extending ClassA.PrintA Method

Do you see that when you instantiate a ClassB but store it in a ClassA variable you get the output from the ClassA version of the function, whereas new ClassB.printA() returns the output from the ClassB version?


It sounds to me like you want to have ClassB.PrintA() output different from ClassA.PrintA(), but you want to be able to call ClassA.PrintA() from an instance of ClassB too. You can do it by declaring ClassA.PrintA() as virtual:

public class classA
{
    public virtual void printA()
    {
        Console.WriteLine("ClassA-PrintA Method");
    }
}

and overriding the function in ClassB:

class ClassB : classA
{
    public void printB()
    {
        Console.WriteLine("ClassB-PrintB Method");
    }

    public override void printA()
    {
        Console.WriteLine("Classb-PrintA Method");

        // Let's also call the ClassA version, just to show you how it works
        base.printA();
    }
}

And just to prove the point, we've called the ClassA version too using the base keyword.

void Main()
{
    new ClassB().printA();
}

Console output:

Classb-PrintA Method

ClassA-PrintA Method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM