none
c# Basic OOPS question RRS feed

  • Question


  • My full program
    ----------------
        public class Parent
        {
            public Parent()
            {
                Console.Write("Parent1 ctor");
            }
    
            public virtual void SaveData()
            {
            
            }
        }
    
        public class Child : Parent
        {
            public Child()
            {
                Console.Write("Child ctor");
            }
    
            public  override void SaveData()
            {
    
            }
        }
    
        public class User
        {
            public void Show()
            {
                Parent p1 = new Child();
                p1.SaveData();
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                User u = new User();
                u.Show();
            }
        }


        
        1) Parent p1 = new Child();  what is the meaning of this line ?
           any term exist in oops to denote the above line?  
        
        2) below code is calling SaveData function of child class instead of parent why. in the below code child object is saved in parent then it suppose to call parent SaveData function.
        please explain.
        
           Parent p1 = new Child();
           p1.SaveData();
           
           
        3) the moment i remove virtual and override keyword from parent and child class function then the same below line of code calling function of parent class....please explain why?
           
           Parent p1 = new Child();
           p1.SaveData();
    Tuesday, May 15, 2018 12:25 PM

Answers

  • First of all: Simplify your program:

    1) As Child is a class derived from Parent, you can assign it like this. This enables polymorphism.

    2) Use a different sample:

    namespace ConsoleCS
    {
        using System;
    
        public class Parent
        {
            public Parent() { Console.WriteLine("Parent1 ctor"); }
            public virtual void Method1() { Console.WriteLine("Parent Method1."); }
            public void Method2() { Console.WriteLine("Parent Method2."); }
            public void Method3() { Console.WriteLine("Parent Method3."); }
        }
    
        public class Child : Parent
        {
            public Child() { Console.WriteLine("Child ctor"); }
            public override void Method1() { Console.WriteLine("Child Method1."); }
            public void Method2() { Console.WriteLine("Child Method2."); }
            public new void Method3() { Console.WriteLine("Child Method3."); }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Console.WriteLine("Parent");
                Parent p = new Child();
                p.Method1();
                p.Method2();
                p.Method3();
    
                Console.WriteLine("Child");
                Child c = new Child();
                c.Method1();
                c.Method2();
                c.Method3();
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
        }
    
    }
    

    which produces this output:

    The key is the keyword virtual which allows in this polymorphic scenario to call the child method, even when you have assigned it to the base type. The keywords new and override only exists to clearly express the intended use-case. Without it compiles, but you get warnings. E.g. to see the actual polymorphism:

    namespace ConsoleCS
    {
        using System;
    
        public class Shape
        {
            public Shape() { Console.WriteLine("Shape ctor"); }
            public virtual void Draw() { Console.WriteLine("Shape Draw()."); }
        }
    
        public class Cricle : Shape
        {
            public Cricle() { Console.WriteLine("Cricle ctor"); }
            public override void Draw() { Console.WriteLine("Cricle Draw()."); }
        }
    
        public class Triangle : Shape
        {
            public Triangle() { Console.WriteLine("Triangle ctor"); }
            public override void Draw() { Console.WriteLine("Triangle Draw()."); }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Shape s;
                s = new Cricle();
                s.Draw();
                s = new Triangle();
                s.Draw();
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
        }
    }
    

    We call (Shape)s.Draw(). But the correct concrete method is called. Obviously the base class cannot know how to draw them.

    3. See 2. override and new are "only" syntactic sugar to clearly express your intention. The key is virtual.

    Tuesday, May 15, 2018 12:54 PM
  • "looking" won't work. You need to understand it.

    I gave you already the necessary explanation, why virtual has effects (possible compiler errors) and why new and override have not (possible compiler warnings).

    And as I also said twice: Your samples are not good samples. Thus it's hard for you to see what happens. This especially means that you're not doing enough tests to compare different outcomes with the according source.

    Would you have done this, thus run my last sample and compared it to your, it should be obvious that the keyword virtual is missing.

    And my sample could not have been more obvious:

    vs.


    Thursday, May 17, 2018 11:42 AM

All replies

  • First of all: Simplify your program:

    1) As Child is a class derived from Parent, you can assign it like this. This enables polymorphism.

    2) Use a different sample:

    namespace ConsoleCS
    {
        using System;
    
        public class Parent
        {
            public Parent() { Console.WriteLine("Parent1 ctor"); }
            public virtual void Method1() { Console.WriteLine("Parent Method1."); }
            public void Method2() { Console.WriteLine("Parent Method2."); }
            public void Method3() { Console.WriteLine("Parent Method3."); }
        }
    
        public class Child : Parent
        {
            public Child() { Console.WriteLine("Child ctor"); }
            public override void Method1() { Console.WriteLine("Child Method1."); }
            public void Method2() { Console.WriteLine("Child Method2."); }
            public new void Method3() { Console.WriteLine("Child Method3."); }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Console.WriteLine("Parent");
                Parent p = new Child();
                p.Method1();
                p.Method2();
                p.Method3();
    
                Console.WriteLine("Child");
                Child c = new Child();
                c.Method1();
                c.Method2();
                c.Method3();
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
        }
    
    }
    

    which produces this output:

    The key is the keyword virtual which allows in this polymorphic scenario to call the child method, even when you have assigned it to the base type. The keywords new and override only exists to clearly express the intended use-case. Without it compiles, but you get warnings. E.g. to see the actual polymorphism:

    namespace ConsoleCS
    {
        using System;
    
        public class Shape
        {
            public Shape() { Console.WriteLine("Shape ctor"); }
            public virtual void Draw() { Console.WriteLine("Shape Draw()."); }
        }
    
        public class Cricle : Shape
        {
            public Cricle() { Console.WriteLine("Cricle ctor"); }
            public override void Draw() { Console.WriteLine("Cricle Draw()."); }
        }
    
        public class Triangle : Shape
        {
            public Triangle() { Console.WriteLine("Triangle ctor"); }
            public override void Draw() { Console.WriteLine("Triangle Draw()."); }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Shape s;
                s = new Cricle();
                s.Draw();
                s = new Triangle();
                s.Draw();
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
        }
    }
    

    We call (Shape)s.Draw(). But the correct concrete method is called. Obviously the base class cannot know how to draw them.

    3. See 2. override and new are "only" syntactic sugar to clearly express your intention. The key is virtual.

    Tuesday, May 15, 2018 12:54 PM
  • Sir see the code

        public class Parent
        {
            public Parent()
            {
                Console.Write("Parent1 ctor");
            }
    
            public void SaveData() //virtual
            {
            
            }
        }
    
        public class Child : Parent
        {
            public Child()
            {
                Console.Write("Child ctor");
            }
    
            public new void SaveData() //override
            {
    
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Parent c = new Child();
                c.SaveData();
            }
        }

    when i run the code it always call the parent SaveData method......why? in my child class i use new operator but still it is calling parent SaveData method

    when i create child class instance

                Child c = new Child();
                c.SaveData();

    then child class SaveData function is getting called but the moment

    i store child class instance in parent type then parent class function is getting called.....which is not clear to me.

    so sir would please explain it. thanks


    • Edited by Sudip_inn Wednesday, May 16, 2018 12:59 PM
    Wednesday, May 16, 2018 12:56 PM
  • Again, please change your sample to more verbose one..

    namespace ConsoleCS
    {
        using System;
    
        public class ParentNonVirtual
        {
            public ParentNonVirtual() { Console.WriteLine("Parent ctor"); }
            public void SaveData() { Console.WriteLine("Parent SaveData"); }
        }
    
        public class ChildNonVirtual : ParentNonVirtual
        {
            public ChildNonVirtual() { Console.WriteLine("Child ctor"); }
            public new void SaveData() { Console.WriteLine("Child SaveData"); }
        }
    
        public class ParentVirtual
        {
            public ParentVirtual() { Console.WriteLine("Parent ctor"); }
            public virtual void SaveData() { Console.WriteLine("Parent SaveData"); }
        }
    
        public class ChildVirtual : ParentVirtual
        {
            public ChildVirtual() { Console.WriteLine("Child ctor"); }
            public override void SaveData() { Console.WriteLine("Child SaveData"); }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                ParentNonVirtual nv = new ChildNonVirtual();
                nv.SaveData();
                ParentVirtual v = new ChildVirtual();
                v.SaveData();
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
        }
    }
    

    Wednesday, May 16, 2018 2:44 PM
  • when i run the code it always call the parent SaveData method......why? in my child class i use new operator but still it is calling parent SaveData method

    when i create child class instance

                Child c = new Child();
                c.SaveData();

    then child class SaveData function is getting called but the moment

    i store child class instance in parent type then parent class function is getting called.....which is not clear to me.

    Sir i am looking for explanation not looking for code example. thanks

    Thursday, May 17, 2018 8:55 AM
  • "looking" won't work. You need to understand it.

    I gave you already the necessary explanation, why virtual has effects (possible compiler errors) and why new and override have not (possible compiler warnings).

    And as I also said twice: Your samples are not good samples. Thus it's hard for you to see what happens. This especially means that you're not doing enough tests to compare different outcomes with the according source.

    Would you have done this, thus run my last sample and compared it to your, it should be obvious that the keyword virtual is missing.

    And my sample could not have been more obvious:

    vs.


    Thursday, May 17, 2018 11:42 AM