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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:26:03+00:00 2026-06-12T04:26:03+00:00

I have a Base class with a method that a child class will almost

  • 0

I have a Base class with a method that a child class will almost always override. However, instead of replacing the base class’ method entirely, I would like for whatever is derived in the child class to be added to what is already in the base class.

For Example:

class BaseClass{

public string str()
 {
   return "Hello my name is" ;
 }
}

class ChildClass : BaseClass{

public override string str() 
 {
   return "Sam";
 }
}

The point is that if I want to access the str() method by creating an instance of the ChildClass, the string will print out as “Hello, my name is Sam”.

I’ve been looking around and all I have been finding is that this should NOT happen, as the base class shouldn’t even know that it is being inherited. So, if the design is false, how would I go about doing this? Keep in mind that there will be multiple child classes inheriting from BaseClass.

Thank you

  • 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-12T04:26:04+00:00Added an answer on June 12, 2026 at 4:26 am

    If you always want to do this, and you don’t want to rely on implementers of the derived classes to remember to override the method and call into the base class, you can use a template pattern:

    public abstract class BaseClass
    {
        public string str()
        {
            return "Hello my name is " + Name;
        }
    
        protected abstract string Name { get; }
    }
    

    Now any non-abstract class that inherits BaseClass will be required by the compiler to override Name, and that value can be consumed by BaseClass.

    public class ChildClass : BaseClass
    {
        protected override string Name
        {
            get { return "Sam"; }
        }
    }
    

    Obviously, this depends on the base class being abstract.

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

Sidebar

Related Questions

I have a base class foo that will be used in multiple child classes
I have created a gem that adds a new method to the class ActiveRecord::Base.
In my base class I have a generic method (ideally this would be a
I have an abstract immutable base class that defines enforces child classes to be
I would to block child classes from overriding a base method and have the
I have a abstract base C# class with a couple of methods that has
I have a PHP class with a method. In the base class (it's more
I have an abstract method in a base class declared like so... public abstract
I have an ActiveRecord before_save method throwing an odd error: class MyThing < ActiveRecord::Base
Whenever i override a method of a base class, other than my implementation of

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.