0

In the following example:

class Base
{
    public void TestMethod()
    {
        Console.WriteLine("Base Class");
    }
}

class Derived : Base
{
    public void DerivedTestMethod()
    {
        Console.WriteLine("Derived Class");
    }
}

class Demo 
{
    public static void Main()
    {
        Base Obj = new Derived();
        Obj.DerivedTestMethod(); //Error Line
    }
}

Why can't I access the method DerivedTestMethod() in Derived class when I have created the object for Derived class using new?

Abbas
  • 14,186
  • 6
  • 41
  • 72
Random User
  • 355
  • 4
  • 8
  • 19

6 Answers6

4

Your object is of type Derived, meaning that it does indeed have the method DerivedTestMethod.

Your problem is that the reference to your object is of type Base. Base doesn't have this method and thus the compiler cannot guarantee that calling DerivedTestMethod will make sense as it's not certain that the reference is of type Derived.


Here's a code example, try using the var keyword, and then newing up the Derived class:

class Demo 
{
public static void Main()
    {
        var Obj = new Derived();
        Obj.DerivedTestMethod();
    }
}

If however you still want to explicitely declare it first to the type Base, or when you recieve an object that is in this Base type then you can test whether the variable Obj is in fact of the class Derived by using the C# is keyword

if(Obj is Derived) // testing with the "is" keyword
{
    Obj = (Derived)obj; // here we cast it
} 
else
{
    // Other code, but now we know that the "Obj" variable isn't of type "Derived".
}

For more information on testing with the "is" keyword, see the MSDN documentation http://msdn.microsoft.com/en-us/library/scekt9xw.aspx or check this answer: https://stackoverflow.com/a/10416231/1155847

Community
  • 1
  • 1
BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56
1

Because you're reference is of type Base. Although you're instantiating an object of type Derived, the reference to your object is of type Base and Base does not contain DerivedTestMethod.

So, althogh in memory you have an object of type Derived and you could cast to it and use it as Derived, the CLR will let you call only methods available for the type of reference (Base, in your case).

Bogdan Beda
  • 758
  • 5
  • 12
0

The other answers all contain significant dangers and omissions (e.g. just trying to cast without testing might be dangerous - unless you really really know that you can cast to the given type).

Read through this answer in full to get a feel and understanding/insight as to why things weren't happening as you'd wanted it to be.

Because you declared it specifcally to the "base" class, try using the "var" keyword, and then newing up the "Derived" class.

class Demo 
{
public static void Main()
    {
        var Obj = new Derived();
        Obj.DerivedTestMethod();
    }
}

If however you still want to explicitely declare it first to "Base"

Base Obj = new Derived();

then you can test whether the variable "Obj" is in fact of the class "Derived" by using the C# "is" keyword

if(Obj is Derived)
{
    Obj = (Derived)obj; // here we cast it
} 
else
{
    // Other code, but now we know that the "Obj" variable isn't of type "Derived".
}

For more information on testing with the "is" keyword, see the MSDN documentation http://msdn.microsoft.com/en-us/library/scekt9xw.aspx or check this answer: https://stackoverflow.com/a/10416231/1155847

Community
  • 1
  • 1
Yves Schelpe
  • 3,343
  • 4
  • 36
  • 69
0

With Base Obj = new Derived(); you are creating a new Derived-Object and then assign it to an Object of type Base. The Base-Class doesn't have the method.

If you want to call the method on the Base-Object you can do:

((Derived)Obj).DerivedTestMethod();
Tomtom
  • 9,087
  • 7
  • 52
  • 95
  • please be more elaborate, and inform the user (as I did in my answer stackoverflow.com/a/21376436/1155847) that casting like that can hold certain dangers and that there's a way of testing whether one can cast a variable to a certain type, by using the keyword "is". – Yves Schelpe Jan 27 '14 at 09:03
0

The following should work for you:

Base Obj = new Derived();
((Dervived)Obj).DerivedTestMethod(); //Error Line

Basically, you are instantiating the base and calling a method from Derived, but you should be able to cast easily enough.

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • please be more elaborate, and inform the user (as I did in my answer stackoverflow.com/a/21376436/1155847) that casting like that can hold certain dangers and that there's a way of testing whether one can cast a variable to a certain type, by using the keyword "is". – Yves Schelpe Jan 27 '14 at 09:04
  • There certainly is a way t test if the variable can be cast. However, in this circumstance, I would argue that the resulting runtime error if it cannot would be preferable. – Paul Michaels Jan 27 '14 at 09:06
  • It's not about that, it's about informing the user here asking and delivering a whole context answer & telling them there's pitfalls in the casting only solution. And that one is able to test before casting without it resulting in runtime errors.. Because sometimes errors aren't "preferable". – Yves Schelpe Jan 27 '14 at 09:21
0

You can achieve it as follows:

  Derived Obj = new Derived();
  Obj.DerivedTestMethod(); 

OR

  Base Obj = new Derived();
  ((Derived)Obj).DerivedTestMethod();  
Jatin patil
  • 4,252
  • 1
  • 18
  • 27
  • please be more elaborate, and inform the user (as I did in my answer http://stackoverflow.com/a/21376436/1155847) that casting like that can hold certain dangers and that there's a way of testing whether one can cast a variable to a certain type, by using the keyword "is". – Yves Schelpe Jan 27 '14 at 09:02