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 8883033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:43:06+00:00 2026-06-14T20:43:06+00:00

New to OOP and I’m confused by how derived-class constructors work when inheriting from

  • 0

New to OOP and I’m confused by how derived-class constructors work when inheriting from a base class in C#.

First the base class:

class BaseClass
{
    private string BaseOutput = null;

    public BaseClass(string BaseString)
    {
        BaseOutput = BaseString;
    }

    public virtual void PrintLine()
    {
        Console.WriteLine(BaseOutput);
    }
}

Here is the derived class:

class SubClass : BaseClass
{
    private string SubOutput = null;

    public SubClass(string BaseString, string SubString) : base(BaseString)
    {
        SubOutput = SubString;
    }

    public override void PrintLine()
    {
        Console.WriteLine(SubOutput);
    }
}

Finally, the main part of the program:

class Program
{
    static void Main(string[] args)
    {
        BaseClass theBase = new BaseClass("Text for BaseClass");
        SubClass theSub = new SubClass("2nd param", "Text for SubClass");

        theBase.PrintLine();
        theSub.PrintLine();

        Console.ReadKey();
    }
}

What I don’t get is why, when calling the constructor for the derived class, I have to also pass the parameter that the base class needs. Shouldn’t the BaseOutput field in the derived class just stay set to null if no value is assigned to it? Why can’t something like this work:

public SubClass(string SubString) : base(BaseString)

Furthermore, when calling the constructor in this derived class, the first parameter has to be named the same as the one in the base class or else it throws an error. If I were to define a new string variable called AnotherString in the derived class, why wouldn’t this work:

public SubClass(string AnotherString, string SubString) : base(BaseString)

Lastly, when you do this the right way and type out this…

public SubClass(string BaseString, string SubString) : base(BaseString)

…what is the first parameter in the SubClass constructor used for? It’s not being assigned or used in any methods for my derived class. Why do I have to even put it there at all?

  • 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-06-14T20:43:08+00:00Added an answer on June 14, 2026 at 8:43 pm

    As to why you can’t do:

    public SubClass(string SubString) : base(BaseString)
    

    What would BaseString be?

    You could do:

    public SubClass(string SubString) : base("SomeFixedString")
    

    or

    public SubClass(string SubString) : base(SubString)
    

    But if you want to pass one string to the base class constructor’s parameter and have an additional one, you’ll need to accept two parameters.

    As to keeping the same name, you don’t. You could do:

    public SubClass(string AnotherString, string SubString) : base(AnotherString)
    

    As to the last question, the first parameter isn’t doing nothing, it’s being passed to the base class constructor. You could use it for something else if you wanted to.

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

Sidebar

Related Questions

I'm new to OOP in PHP, is that to seems correct ? class whatever
I am new to OOP and have a class, inside this class I am
I am pretty new to OOP with PHP. OK first of all this produces
I'm new to OOP and I'm confused about this following line on chapter 6
I'm new to OOP and could use some help. My class is not working:
I'm quite new to OOP and I'm concerned that this class that I've written
I'm new to OOP. I created this class called Site that is extended but
I am new to OOP and I am confused how should I proceed. My
I'm new to OOP and just wanted to know, within a class, when should
New to OOP perl...First program, not bale to overload constructor. I have tried many

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.