3
namespace AbstractImplementation
{
    public class baseclass 
    {
        public virtual void testingSealed()
        {
            Console.WriteLine("This is testingSealed base");
        }
    }


    public class derived : baseclass
    {
        public override void testingSealed()
        {
            Console.WriteLine("This is testing derived");
        }

    }

  class Program
    {
        static void Main(string[] args)
        {
            derived der = new derived();
            der.testingSealed(); 

        }
    }
}

This will give output as "This is testing derived". Is it possible to get "This is testingSealed base" from der.testingSealed()?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Pravin
  • 55
  • 4
  • If you want to get the base message, why don't you instantiate the base type? Sounds like the [XY](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) problem to me. – Yuval Itzchakov Dec 25 '15 at 08:12
  • To get access to baaeclass method you have to create object of base class. It is not possible to access the base class object when it is overrided in derived class. – Ankush Bist Dec 25 '15 at 08:15
  • 1
    This question was asked me in an interview. I gave 2 answers in this scenario. 1) Need not to override the method in derived class if we want to access the base 2) create base class object and access the method. But he said there are ways to access base class method which is overridden in derived class via derived class object and when I asked answer for this, he asked me to find it out. – Pravin Dec 25 '15 at 08:18
  • Using `call` MSIL instruction against virtual not sealed method considered verifiable only when instance for call is `this`, so that, it is possible only from inside object, not from outside code. – user4003407 Dec 25 '15 at 08:24

3 Answers3

2

You can access base method from a derived class using base keyword:

public class BaseClass
{
    public virtual void TestingSealed()
    {
        Console.WriteLine("This is testingSealed base");
    }
}


public class Derived : BaseClass
{
    public override void TestingSealed()
    {
        base.TestingSealed(); // here
        Console.WriteLine("This is testing derived");
    }

    public void TestingSealedBase()
    {
        base.TestingSealed(); // or even here
    }
}

var der = new Derived();
der.TestingSealed();
der.TestingSealedBase();

This will output:

This is testingSealed base.
This is testing derived.
This is testingSealed base.

P.S. Please, C# naming conventions require classes and methods to be named in UpperCamelCase.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
1

You cannot, once the derived class is instantiated, call the virtual method in the base class. If you must call the virtual method there is a flaw in your design.

See this post: How can I call the 'base implementation' of an overridden virtual method?

However, you can call the virtual method from the overridden method:

public override void testingSealed()
    {
        base.testingSealed();
        Console.WriteLine("This is testing derived");
    }
Community
  • 1
  • 1
Westerlund.io
  • 2,743
  • 5
  • 30
  • 37
-2

Just do not override the method in the derived class.

Martin Maat
  • 714
  • 4
  • 23
  • Better to add comment when you are not posting any code – Ankush Bist Dec 25 '15 at 08:23
  • 1
    This is the first correct answer to the question and it does not require any code. The world would be a better place if more people would not post stuff that is irrelevant. – Martin Maat Dec 25 '15 at 08:31