I am creating an abstract base class which have functions implemented by other classes. My doubts are as follows
1) Do i need give ‘virtual’ in front of every function which need to be overridden by the child classes? I see some examples without the virtual keyword and still they are open to be overridden.
2) I need to have a function which will be implemented in the base class and i dont want it to be overridden by the child classes. I added the ‘fixed’ keyword infront of that function. Compiler start complaining ‘member’ cannot be sealed because it is not an override. Am i doing any wrong here?
abstract public class ShapeBase
{
private ShapeDetails _shapedDetails;
public CampusCardBase(ShapeDetails shDetails)
{
_shapedDetails= shDetails;
}
public virtual void Draw();
public virtual float getWidth();
public virtual void Swap();
public virtual void Erase();
public sealed ShapeDetails getShapeDetails()
{
return _shapedDetails;
}
};
For methods without implementations in an abstract class, use
abstractas well:Methods aren’t overridable by default; they only allow derived classes to override if declared
abstract,virtualoroverride(but notoverride sealed).Therefore you don’t need to give
getShapeDetails()any other modifiers thanpublic:On a side note, you should stick with .NET naming conventions and capitalize method names using Pascal case, so
getWidth()becomesGetWidth()andgetShapeDetails()becomesGetShapeDetails().In fact you should be using a property getter for your
_shapedDetailsfield instead of agetShapeDetails()method: