0

I have written the code below, but i see that for to access the width and the length for the last child which is badRectangle is by overriding everything inhrerited from the Rectangle and shape class, which means i have to duplicate the input and i i had 6 or more levels of inheritance the code would kind of confuse and repeat a lot of things.

This code works correctly but is the correct way of dealing with inheritance in C#.

class Program
{
    static void Main(string[] args)
    {
       badRectangle myRect = new badRectangle(true,"Rectangle",23.0,23);
       Console.WriteLine("The Area of your Rectangle = " + myRect.getArea().ToString()
           + "\nAnd " + myRect.getStatus());
       Console.ReadLine();
    }
    public abstract class shape
    {
        string type;

        public abstract double getArea();
        public shape(string type)
        {
            this.type = type;
        }
    }
    public class rectangle : shape
    {
        double width, length;
        public rectangle(string type, double width, double length):base(type)
        {
            this.width = width;
            this.length = length;
        }
        public override double getArea()
        {
            return width * length;    
        }
    }

    public class badRectangle : rectangle
    {
        double width, length;
        bool badOrNot = false;
        public badRectangle(bool badOrNot,string type, double width, double length):base(type,width,length)
        {
            this.badOrNot = badOrNot;
            this.width = width;
            this.length = length;
        }

        public string getStatus()
        {
            string answer = "No, Rectangle is not bad";
            if (badOrNot == true)
            {
                answer = "Yes, Rectangle is bad";
            }
                return answer;
        }
        public override double getArea()
        {

            return width * length;
        }
    }
}
3
  • 2
    getArea and getStatus are more a "java thing" than a C# one ; they should be (readonly) properties instead. Commented Jul 24, 2016 at 10:17
  • I would then just call the property and to get its value Commented Jul 24, 2016 at 11:34
  • You've got the ideas right. Here's something interesting to read about: Favor composition over inheritance. A lot of programming instruction covers inheritance, but what they don't tell you is that it's used more than it should be. (Not saying that's the case in your example.) Commented Jul 24, 2016 at 21:11

2 Answers 2

1

This would be the "correct" or conventional way to do this in C#:

public abstract class Shape
{
    public string Type { get; private set; }

    public abstract double Area { get; }

    public Shape(string type)
    {
        this.Type = type;
    }
}

public class Rectangle : Shape
{
    public double Length { get; private set; }
    public double Width { get; private set; }

    public Rectangle(string type, double width, double length)
        : base(type)
    {
        this.Width = width;
        this.Length = length;
    }

    public override double Area { get { return this.Width * this.Length; } }
}

public class BadRectangle : Rectangle
{
    public bool BadOrNot { get; private set; } = false;

    public BadRectangle(string type, double width, double length, bool badOrNot)
        : base(type, width, length)
    {
        this.BadOrNot = badOrNot;
    }

    public string Status
    {
        get
        {
            string answer = "No, Rectangle is not bad";
            if (this.BadOrNot == true)
            {
                answer = "Yes, Rectangle is bad";
            }
            return answer;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why did you make properties for all the fields ? There is nothing stating that they should take part in the public interface (services) of the class. So if they aren't needed publicly, fields are sufficient enough.
@Sehnsucht - That's a fair call. I think it probably is what the OP wanted though, even though they didn't explicitly say so. I'm happy to go either way.
0

You don't need to set width and length in the derived classes again, just pass them to the constructor of the base class. If you need to access them in the derived class, make them protected. The getArea() doesn't have to be overridden if it does the same thing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.