Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:36:14+00:00 2026-05-30T08:36:14+00:00

Im was reading an example of simple inheritence and came across the basic idea

  • 0

Im was reading an example of simple inheritence and came across the basic idea that a square is a base type and a rectangle is derived from a square.

The example for setting the square dimensions used a property called Size.
The example for the rectangle then went on to use Width and Height.

This didnt make sense in my head, so I coded it.

The problem seems to be that when accessing rectangle, there will always be a confusing property called ‘Size‘ present.

Have I got this right? Or is there a way to hide other classes from seeing Size when looking at rectangle?

public class square
{
    public int Size { get; set; }
    public square(int size)
    {
        this.Size = size;
    }
}

public class rectangle : square
{
    public int Width { get { return base.Size; } set { base.Size = value; } }

    public int Height { get; set; }

    public rectangle(int width, int height)
        : base(width)
    {
        Height = height;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:36:15+00:00Added an answer on May 30, 2026 at 8:36 am

    You are 100% right that this is backwards inheritance. Instead, you should have a Square class inherit from a Rectangle class, since a square is a special kind of rectangle.

    Then, you get something like

    public class Rectangle
    {
        public int Width { get; private set; }
        public int Height { get; private set; }
    
        public Rectangle(int width, int height)
        {
            if (width <= 0 || height <= 0)
                throw new ArgumentOutOfRangeException();
            Width = width;
            Height = height;
        }
    }
    
    public class Square : Rectangle
    {
        public int Size
        {
            get
            {
                return Width;
            }
        }
    
        public Square(int size)
            : base(size, size)
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading on a blog that there's a GLImageProcessing example from apple. I
Below is a simple example of how I'm reading from a plist and displaying
I was reading the example chapter from the book by Ayende and on the
I was reading thourg this example (link) ... From the description, this seems paradox
I have the following example of reading from a buffered reader: while ((inputLine =
While reading a book about JavaScript I stumbled across an example: var names =
I am reading a tutorial that uses the following example (that I'll generalize somewhat):
I am trying to get a simple example working from an assembly book I
I'm trying to get a simple example working with GCDAsyncSocket, and am discovering that
Below is a simple example of using pointer in delphi. Type TRecord1 = Record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.