Jesper
Jesper

Reputation: 2094

Error message: does not implement inherited abstract member

Beware, cause I'm newbie with C#! So I have a regular class Cube that inherits from an abstract class Shape3D which in turn inherits from an abstract class Shape2D.

Shape2Dprovides, among other things, two abstract properties: Area & Perimeter. The Cube constructor calls the base class constructor, and sets a new objects length, height and width.

For some reason, I get this error from Cube:

Error: 'Cube' does not implement inherited abstract member 'Shape2D.Area.get'
Error: 'Cube' does not implement inherited abstract member 'Shape2D.Perimeter.get'

This is how Shape2D looks:

abstract class Shape2D : Shape
{
    private double _length, _width;
    public double Length{..}
    public double Width{..}

    public abstract double Area{ get; }

    public abstract double Perimeter{ get; }

    protected Shape2D(ShapeType shapeType, double length, double width) 
    : base(shapeType)
    {
        _length = length;
        _width = width;
    }

    public override string ToString(){..}
}

Shape 3D:

abstract class Shape3D : Shape2D
{
    protected Shape2D _baseShape;
    private double _height;

    public double Height{..}
    public virtual double MantelArea{..}
    public virtual double TotalSurfaceArea{..}
    public virtual double Volume{..}

    protected Shape3D(ShapeType shapeType, Shape2D baseShape, double height)
    : base(shapeType, height, height)
    {
        _baseShape = baseShape;
        _height = height;
    }

    public override string ToString(){..}
}

Cube:

class Cube : Shape3D --> This returns inherited error.
{
    ...
}

I've google my a** off, and haven't found anything useful. Why does my regular class not inherit abstract members from an abstract class? Can anyone please guide me?

Upvotes: 0

Views: 3309

Answers (1)

chris-crush-code
chris-crush-code

Reputation: 1149

Because your 2d class is abstract with two abstract properties, you need to implement them in your concrete class as shown below.

Quick edit to answer your question - The properties are abstract that's why it appears not to 'inherit' them. You need to implement any abstract properties when making a concrete implementation. In case you're thinking you should change these from abstract on your 2dShape, they should probably stay abstract there because different shapes will have different formulas for calculating perimeter & area based on the lengths & widths.

class Cube : Shape3D
{
   public override double Area{ get; }

    public override double Perimeter{ get; }

}

Upvotes: 0

Related Questions