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

  • Home
  • SEARCH
  • 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 7160275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:22:21+00:00 2026-05-28T13:22:21+00:00

Good day everyone. I have a question about making and using derived classes of

  • 0

Good day everyone.

I have a question about making and using derived classes of some base abstract class.

I can’t quite understand how it works.

My goal: Make a basic class that will define what functions there should be, and then create different derived classes. Then to make an array of base class and instantiate in it different derived classes.

For example:

abstract class UIElement
{
    //constructor
    abstract public UIElement();
    //draw element
    abstract public void Draw(int mouseX, int mouseY);
    //checks if the element is pointed at by mouse
    abstract public Boolean IsPointed(int mouseX, int mouseY);
    //defines what happens when clicked
    abstract public void Click(int mouseX, int mouseY);
    //destructor
    abstract public ~UIElement();
}

Then create several derived classes and do like that:

UIElement[] qwe = new UIElement[3];
qwe[0] = new UIElementButton();
qwe[1] = new UIElementLabel();
qwe[2] = new UIElementSomethingElse();

1) What is the proper way to do it, because I can’t find a definitive answer or an article that would explain it…

2) About class variables, where they should be declared: base class or derived?

3) What if different derived classes have different variables, how that should be addressed?

4) Can derived classes have different constructor including different arguments?

  • 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-28T13:22:22+00:00Added an answer on May 28, 2026 at 1:22 pm

    In comments below, questionner is looking for advice on best practices for writing inheritence hierachies with abstract classes

    From a general best-practices and correctness point of view, you ought to declare your UIElement base class without abstract on the constructor and without a finalizer. e.g.

    public abstract class UIElement
    {
        //constructor
        protected UIElement();
        //draw element
        public abstract void Draw(int mouseX, int mouseY);
        //checks if the element is pointed at by mouse
        public abstract bool IsPointed(int mouseX, int mouseY);
        //defines what happens when clicked
        public abstract void Click(int mouseX, int mouseY);
    }
    

    You could then implement derived classes as follows

    public class UIElementButton : UIElement
    {
        // Optional - create parameterless constructor and
        // explicitly call base constructor
        public UIElementButton() : base()
        {
        } 
    
        // Mandatory - must override abstract methods
        public override Draw(int mouseX, int mouseY)
        {
        }
    
        // Mandatory - must override abstract methods
        public override Boolean IsPointed(int mouseX, int mouseY)
        {
        }
    
        // Mandatory - must override abstract methods
        public override void Click(int mouseX, int mouseY)
        {
        }
    }
    

    You are correct that if you define derived classes as per the above, you could instantiate elements and store in an array of the base type as follows:

    UIElement[] qwe = new UIElement[3]; 
    qwe[0] = new UIElementButton(); 
    qwe[1] = new UIElementLabel(); 
    qwe[2] = new UIElementSomethingElse(); 
    
    qwe[0].Click(1, 2); // Invokes UIElementButton.Click
    qwe[1].Draw(3, 4);  // Invokes UIElementLabel.Draw
    

    You should also be aware there is a UIElement class defined in WPF. This won’t be a problem if you define a namespace for your type, but perhaps consider a more explicit type name such as BaseElement to distinguish your custom type.


    Regarding your queries in comments:

    For further advice on usage of abstract classes, please see All about abstract classes – Codeproject. – Where should class variables be declared: base class or derived?
    – What if different derived classes have different variables, how that should be addressed?
    – Can derived classes have different constructor including different arguments?

    These are really the topic for different questions or own research on google, however as a starter I can say:

    1. Class variables should be declared in the scope where they are needed. Meaning if you have a variable Guid _uiElementId and want it to be accessed by derived types, it should be declared in the base class and marked as protected. If you have a variable in UIElementButton called Point _mousePoint and only UIElement button needs it, leave it in there and mark as private

    2. That’s fine – any type can see its own private variables and protected variables from base classes. Just make sure names don’t clash

    3. Yes they can, for instance.


     public class UIElementButton : UIElement    
     {    
        private string _buttonText; 
    
        // Overloaded ctor
        public UIElementButton(string buttonText) : base()
        {
            buttonText = buttonText;
        } 
    
        // Default constructor. Mark as private or protected 
        // to hide from external API
        public UIElementButton() : base()
        {
        } 
    }
    

    Best regards,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day everyone. I have an audio class, that plays a .wav file. But
Good day everyone. I have a question regarding drawing libraries for C#. I always
Good day everyone, I have a directory set up with some Apache Auth stuff.
Good day everyone, I am building a page in ASP.NET, and using Master Pages
Good day. I'm using the WebClient class in my C# application in order to
Good Day everyone!! One question.. or two.. Is it possible to use Expression Blend
Good day everyone, I have the following XML data : <?xml version=1.0 encoding=utf-8 ?>
Good day everyone, i have faced with such an issue as linkage error like
Good Day Everyone, I have a program (lets call it 'A'), which is called
Good day, I have a gen_server process which does some long-running state-updating tasks periodically

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.