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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:30:19+00:00 2026-06-11T12:30:19+00:00

When you read this you’ll be awfully tempted to give advice like this is

  • 0

When you read this you’ll be awfully tempted to give advice like “this is a bad idea for the following reason…”

Bear with me. I know there are other ways to approach this. This question should be considered trivia.

Lets say you have a class “Transaction” that has properties common to all transactions such as Invoice, Purchase Order, and Sales Receipt.

Let’s take the simple example of Transaction “Amount”, which is the most important monetary amount for a given transaction.

public class Transaction
{
    public double Amount { get; set; }

    public TxnTypeEnum TransactionType { get; set; }
}

This Amount may have a more specific name in a derived type… at least in the real world. For example, the following values are all actually the same thing:

  • Transaction – Amount
  • Invoice – Subtotal
  • PurchaseOrder – Total
  • Sales Receipt – Amount

So now I want a derived class “Invoice” that has a Subtotal rather than the generically-named Amount. Ideally both of the following would be true:

  1. In an instance of Transaction, the Amount property would be visible.
  2. In an instance of Invoice, the Amount property would be hidden, but the Subtotal property would refer to it internally.

Invoice looks like this:

public class Invoice : Transaction
{
    new private double? Amount
    {
        get
        {
            return base.Amount;
        }
        set
        {
            base.Amount = value;
        }
    }

    // This property should hide the generic property "Amount" on Transaction
    public double? SubTotal
    {
        get
        {
            return Amount;
        }
        set
        {
            Amount = value;
        }
    }

    public double RemainingBalance { get; set; }
}

But of course Transaction.Amount is still visible on any instance of Invoice.

Thanks for taking a look!

  • 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-11T12:30:20+00:00Added an answer on June 11, 2026 at 12:30 pm

    Thanks for all the help.

    OK, of course you cannot “hide” public properties on the base class when the derived class IS a base instance. Somewhere deep in my brain I already knew that. Doh!

    I wound up getting the syntactic sugar to behave the way I wanted for the consumer by using a third class called TransactionBase. This class is abstract and contains the shared, non-aliased stuff that exists for all transactions like currency, exchange rate, created/modified date and time, transaction date, etc… in addition to aliased stuff like Amount.

    Here, I just show the Amount property in question:

    public abstract class TransactionBase
    {
        protected virtual double Amount { get; set; }
    }
    

    Then Transaction looks like this:

    public class Transaction : TransactionBase
    {
        public new double Amount 
        { 
            get
            {
                return base.Amount;
            }
            set
            {
                base.Amount = value;
            }
        }
    }
    

    And Invoice:

    public class Invoice : TransactionBase
    {
        public double SubTotal
        {
            get
            {
                return Amount;
            }
            set
            {
                Amount = value;
            }
        }
    }
    

    And access works the way I wanted:

    var transaction = new Transaction();
    
    // This line works fine:
    var transactionAmount = transaction.Amount;
    
    
    
    var invoice = new Invoice();
    
    // This line works fine:
    var invoiceSubtotal = invoice.SubTotal;
    
    // This line won't compile.
    // Error: TransactionBase.Amount is inaccessible due to its protection level.
    var invoiceAmount = invoice.Amount;
    

    So the answer to my original question was, “no” you cannot hide public inherited members. The above solution fakes it with accessing the protected member directly in the derived types, but it still sort of sucks. Too much typing.

    Of course, now that I fiddled and piddled with all that, I’m seeing that a better solution throws out the protected members altogether and saves me some typing. By the way, YES I am embarrassed that I didn’t jump immediately to this solution.

    EDIT: Actually, the first appraoch in my answer might be better. With the 2nd one, I’d lose the “Amount” or “Subtotal” when casting from a Transaction to an Invoice.

    public abstract class TransactionBase
    {
        // There are some shared properties here.
    }
    
    public class Transaction : TransactionBase
    {
        public double Amount { get; set; }
    }
    
    public class Invoice : TransactionBase
    {
        public double SubTotal { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this: http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns I like the idea, want to use the extension. I
Having read this past question for git, I would like to ask if there
I read this answer which made it sound like when a shared value is
I read this question here: Is there a way to override the empty constructor
I read this , and I understand it, but, is there a way to
I read this article, it suggests (page 1025 last paragraph) that there is a
I read this answer and its comments and I'm curious: Are there any reasons
I read this question to know how to send email in Android using ACTION_SEND:
Having read this article and many others out there on how to not store
I read this advice from error message: You should consider either expiring and/or testing

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.