locked
C# OOPs New and override both keyword used in same place RRS feed

  • Question

  • User264732274 posted

    read one code example from here http://stackoverflow.com/a/16104665/508127  and bit confuse about the code execution flow.

    the below explanation is not clear. so help me to understand with easy code example.

        When you mark a class member as virtual it can be override in the sub classes.
    
        If you want to change a method (which was declared virtual in base class) in sub class you can use both new and override keywords but there is a difference between them
    
        . When using new: if you cast an object of sub class as base class, then call that method, base class implementation will run
    
        . When using override: if you cast an object of sub class as base class, then call that method, sub class implementation will run.
    
    Here is the code
    
    AbstractClass instance = new DerivedClass();
    instance.SayGoodbye();  //See you later - In derived class I'm in  NEW  member
    
    But if you use override
    
    AbstractClass instance = new DerivedClass();
    instance.SayGoodbye();  //Goodbye- abstract member \n
    

    specially this area is not clear. have a look and help me to understand which instance is creating when.

    AbstractClass instance = new DerivedClass();
    instance.SayGoodbye();  //See you later - In derived class I'm in  NEW  member
    
    But if you use override
    
    AbstractClass instance = new DerivedClass();
    instance.SayGoodbye();  //Goodbye- abstract member \n

    thanks

    Tuesday, February 9, 2016 11:23 AM

Answers

  • User-84896714 posted

    Hi sudip_inn,

    Both of them will create a instance of DerivedClass and referenced as AbstractClass. When we create a instance of a class, it will allocate memory for all the fields of this class. But we don't need to allocate memory of methods for different instances. All the instances will share class methods. When we call a method of an instance. It will use two types of rules.

    If the method doesn't start with virtual? It will call the method of a type which we used to define the instance. For example.

    AbstractClass instance;
    

    If the method start with virtual? It will call the method of a type which we used to create the instance. For example.

    instance = new DerivedClass();

    Best Regards,
    Li Wang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 9, 2016 12:40 PM
  • User-1664007096 posted

    Hi sudip_inn,

    According to your description, as far as we know that new Modifier that used to hide an inherited member from a base class member, you could refer to below:

    class AbstractClass
    {
        public virtual void SayGoodbye()
        {
            Console.WriteLine("See you later ");
        }
    }
    class OverrideDerivedClass : AbstractClass
    {
        public override void SayGoodbye()
        {
            Console.WriteLine("Goodbye");
        }
    }
    class NewDerivedClass : AbstractClass
    {
        public new void  SayGoodbye()
        {
            Console.WriteLine("Goodbye");
        }
    }
    static void Main(string[] args)
    {
        AbstractClass instance = new OverrideDerivedClass();
        //then output Goodbye
        instance.SayGoodbye();
        instance = new NewDerivedClass();
        //then output See you later
        instance.SayGoodbye();
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 8, 2016 7:32 AM
  • User-470152798 posted

    using System;
    class abstractclass
    {
    public virtual void method()
    {
    Console.WriteLine("from virtual method");
    }
    }
    class overrideDerived : abstractclass
    {
    public override void method1()
    {
    Console.WriteLine("from override method");
    }


    public new override void method2()
    {
    Console.WriteLine("from new override method");
    }
    }

    class class1
    {
    static void Main(string[] args)
    {
    abstractclass ins = new abstractclass();
    ins.method();
    overrideDerived on = new overrideDerived();
    on.method1();
    on.method2();

    }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 8, 2016 9:50 AM

All replies

  • User-84896714 posted

    Hi sudip_inn,

    Both of them will create a instance of DerivedClass and referenced as AbstractClass. When we create a instance of a class, it will allocate memory for all the fields of this class. But we don't need to allocate memory of methods for different instances. All the instances will share class methods. When we call a method of an instance. It will use two types of rules.

    If the method doesn't start with virtual? It will call the method of a type which we used to define the instance. For example.

    AbstractClass instance;
    

    If the method start with virtual? It will call the method of a type which we used to create the instance. For example.

    instance = new DerivedClass();

    Best Regards,
    Li Wang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 9, 2016 12:40 PM
  • User264732274 posted

    thanks for reply. can u explain with full small working sample code.

    this concept is not clear in OOPS "create a instance of DerivedClass and referenced as AbstractClass"

    what is the meaning of below code

    AbstractClass instance = new DerivedClass();

    what it is called in OOPS.......what the above code try to do ? when people use this kind of coding approach ?

    please share the knowledge. thanks

    Wednesday, February 10, 2016 10:33 AM
  • User-1664007096 posted

    Hi sudip_inn,

    According to your description, as far as we know that new Modifier that used to hide an inherited member from a base class member, you could refer to below:

    class AbstractClass
    {
        public virtual void SayGoodbye()
        {
            Console.WriteLine("See you later ");
        }
    }
    class OverrideDerivedClass : AbstractClass
    {
        public override void SayGoodbye()
        {
            Console.WriteLine("Goodbye");
        }
    }
    class NewDerivedClass : AbstractClass
    {
        public new void  SayGoodbye()
        {
            Console.WriteLine("Goodbye");
        }
    }
    static void Main(string[] args)
    {
        AbstractClass instance = new OverrideDerivedClass();
        //then output Goodbye
        instance.SayGoodbye();
        instance = new NewDerivedClass();
        //then output See you later
        instance.SayGoodbye();
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 8, 2016 7:32 AM
  • User-470152798 posted

    using System;
    class abstractclass
    {
    public virtual void method()
    {
    Console.WriteLine("from virtual method");
    }
    }
    class overrideDerived : abstractclass
    {
    public override void method1()
    {
    Console.WriteLine("from override method");
    }


    public new override void method2()
    {
    Console.WriteLine("from new override method");
    }
    }

    class class1
    {
    static void Main(string[] args)
    {
    abstractclass ins = new abstractclass();
    ins.method();
    overrideDerived on = new overrideDerived();
    on.method1();
    on.method2();

    }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 8, 2016 9:50 AM