Abstract classes and virtual methods

Abstract classes can be overridden by virtual methods (true, false)

Questions by Raj_Kumar12

Showing Answers 1 - 4 of 4 Answers

SoCalBruce

  • Dec 3rd, 2007
 

An abstract class can ONLY be overridden by a virtual method.  If the method is not virtual then the abstract method is "hidden".

 

The "override" also means the member is virtual.  For example, Derived.Method is a virtual method:

 
public abstract class Base
{
    public abstract void Method ( );
}
 
public class Derived : Base
{
    public override void Method ( )
    {
    }
}

  Was this answer useful?  Yes

PeterVH

  • Aug 19th, 2011
 

Hello Raj,

you override methods, not classes. So, guessing you mean abstract methods.
The overriding method can NEVER be virtual, but by using the "new" keyword you can *replace* the abstract method, not override. You can override an abstract method with another abstract method however.

Basically:
The overridden base method must be virtual, abstract, or an "override" itself.
The overriding (derived) method cannot use new / static / virtual.

Code
  1. span style="font-style: italic;">// The overridden base method must be virtual, abstract, or override.// I don't know, let's just return a random number// could also make this class abstract, but no need for it// needs the override to implement, otherwise won't compile// you can leave out the override here (will compile),

  2.         // but then you aren't overriding but hiding. In that case add "new" keyword// Needs to be abstract, as it has an abstract member// you can override an abstract method with another abstract one

  3.         // (for what use I still don't really know..

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions