简体   繁体   中英

Is it possible to call method of an Abstract class in derived class or any other class

Is it possible to call method of an Abstract class in derived class or any other class. My code is below, I want to call Abstr's Describe() method in Program's Main method. Is it possible? if answer is yes how?

class Program
{
    public void Main()
    {
        //I want to call the Describe() method here, how do i do that
        Console.ReadLine();
    }
}

public abstract class Abstr
{
    public void Describe()
    {
        //do something
    }
}

Since your method is not static, you need to initialize a variable from that abstract class and call that method from it. To do that you may inherit the abstract class by concreate class and then call that method. Note that the abstract class can't be initialized throw a constructor like Abstr abstr = new Abstr(); is not valid. So:

public abstract class Abstr
{
    public void Describe()
    {
        //do something
    }
}

public class Concrete : Abstr
{
   /*Some other methods and properties..*/ 
}

class Program
{
    public void Main()
    {
        Abstr abstr = new Concrete();
        abstr.Describe();
        Console.ReadLine();
    }
}

You should be able to use Abstr.Describe() directly. It is a static method, so the class being abstract shouldn't matter.

Edit

Now that the code in the question has been edited and the static keyword is removed on the methods, this answer does no longer apply.

Q: is it possible to call method of an Abstract class in derived class?

A: Sure, as long as it's public, protected (and the same class or a subclass), or internal (and the same assembly)

Q: Any other class?

A: Sure, as long as it's public or internal (and the same assembly)

Good link: http://agsmith.wordpress.com/2007/12/15/internal-and-protected/

It's enough to call

EDIT:

Original post was changed, so this answer is not valid.

 Abstr.Describe();

Regards.

yes it is possible code: here

This works for non static method as well

Yes, so long as the method definition in the supertype is accessable to the subtype (and to the code that wants to invoke the method).

Here's some sample code. Whether the method definition in the subtype or supertype is invoked is dependent upon how the override is defined:

public abstract class AbstractSupertype
{
  public void Alpha()
  {
    Console.WriteLine( "AbstractSupertype.Alpha()" ) ;
    return ;
  }
  public abstract void Bravo() ;
  public virtual  void Charlie()
  {
    Console.WriteLine( "AbstractSupertype.Charlie()" ) ;
    return ;
  }
}

public class ConcreteSubtype : AbstractSupertype
{
  public new void Alpha()
  {
    Console.WriteLine( "ConcreteSubtype.Alpha()" ) ;
  }
  public override void Bravo()
  {
    Console.WriteLine( "ConcreteSubtype.Bravo()" ) ;
  }
  public override void Charlie()
  {
    Console.WriteLine( "ConcreteSubtype.Charlie()" ) ;
  }
}
class Program
{
  static void Main( string[] args )
  {
    ConcreteSubtype   subTypeInstanceReference   = new ConcreteSubtype() ;
    AbstractSupertype superTypeInstanceReference = subTypeInstanceReference ;

    subTypeInstanceReference.Alpha()     ; // invokes subtype's method
    superTypeInstanceReference.Alpha()   ; // invokes supertype's method

    subTypeInstanceReference.Bravo()     ; // invokes subtype's method
    superTypeInstanceReference.Bravo()   ; // invokes subtype's method

    subTypeInstanceReference.Charlie()   ; // invokes subtype's method
    superTypeInstanceReference.Charlie() ; // invokes subtype's method

    return ;
  }
}

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