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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:56:46+00:00 2026-05-26T08:56:46+00:00

I’ll just jump right in. Suppose AudioFile is an abstract class like so: abstract

  • 0

I’ll just jump right in.

Suppose AudioFile is an abstract class like so:

abstract class AudioFile 
{
    public string Title { get; set; }
}

Now, in most cases, using a.Title is perfectly suited for other classes that inherit from AudioFile. However, in the case of MPEG files, this is stored in a different object in the id3 variable:

class MPEG : AudioFile 
{
    private ID3 id3;

    public new string Title {
        get {
            return id3.Title; 
        }
        set {
            id3.Title = value;
        }
    }
}

class WMA : AudioFile
{

}

What I would like is to do the following:

AudioFile a;

if( isMPEG ) {
    a = LoadMPEG(); // Returns a new MPEG instance.
} else
if( isWMA ) {
    a = LoadWMA();  // Return a new WMA instance.
}

Console.WriteLine( a.Title );
// Other stuff with a.

I would expect the output to be, whether an MPEG or a WMA, the song’s title. However, when its MPEG, it doesn’t work as expected (because it isn’t using the id3 object). The only way it works is:

if( isMPEG ) {
    MPEG a = LoadMPEG();    // Returns a new MPEG instance.
    Console.WriteLine( a.Title );
    // Other stuff with a.
} else
if( isWMA ) {
    WMA a = LoadWMA();  // Return a new WMA instance.
    Console.WriteLine( a.Title );
    // Other stuff with a.
}

Which is not what I want. And ideas on how to do what I want to do?

  • 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-26T08:56:47+00:00Added an answer on May 26, 2026 at 8:56 am

    Make the abstract class property virtual so that derived classes can override it if they need different behaviors.

    abstract class AudioFile
    {
        public virtual string Title { get; set; } 
    }
    
    class MpegFile : AudioFile
    {
        public override string Title { /* your custom getter and setter */ }
    }
    
    AudioFile file = new MpegFile();
    string title = file.Title; // will use override
    

    In your version, you leave the abstract class property non-virtual and mark the derived class property as new. This allows you to use your custom behavior only via the reference of the derived class. You lose the ability to experience the polymorphic behavior via the base. The new modifier hides the base behavior only through the derived reference. Base references use the base behavior.

    AudioFile file = new MPEG(); // will use base behavior for non-virtual methods
    MPEG file = new MPEG(); // will use derived behavior when non-virtual methods are hidden by new
    

    You may happen to come from a Java background. In C#, members are not virtual by default. You must mark them as virtual, and use override to replace or augment the base implementations in a polymorphic way. (For abstract members in abstract classes, you would use the keyword abstract instead of virtual.) The new keyword is useful when the base method is not virtual, but you have already seen its limitation.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.