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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:17:43+00:00 2026-06-15T19:17:43+00:00

In the following example I am using C#. I have created an abstract class

  • 0

In the following example I am using C#.

I have created an abstract class Base_Collection, and then I derive other classes from it (Book, Movie, Game). However, I don’t know how to add new methods that can be used for those new children classes without getting a compiler error.

namespace polymorphism_test_01
{
    abstract class Base_Collection
    {
        public int id = 0;
        public string title = "";

        public Base_Collection() { }
    }
}    

namespace polymorphism_test_01
{
    class Book : Base_Collection
    {
        public Book() { }

        public override void Read_Book() { }
    }
}    

namespace polymorphism_test_01
{
    class Game : Base_Collection
    {
        public Game() { }

        public void Play_Game() { }
    }
}   

namespace polymorphism_test_01
{
    class Movie : Base_Collection
    {
        public Movie() { }

        public void Watch_Movie() { }
    }
}

Calling book.Read_Book() doesn’t work in the following snippet, the compiler will output an error:

namespace polymorphism_test_01
{
    class Program
    {
        public static void Main(string[] args)
        {
            Base_Collection book = new Book();

            book.title = "The Dark Tower";
            book.Read_Book();

            Console.WriteLine(book.title);
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
  • 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-15T19:17:44+00:00Added an answer on June 15, 2026 at 7:17 pm

    Further to the answer from @Will (which is correct), you could structure your heirarchy a little better. For example, you do something different with each of the media types, but each type still gets consumed.

    So change your base class to have an abstract base method called Consume(), which is then overridden and actually implemented in each derived class, this way you can pass around instances of your base class and there is no need for casting to invoke the Consume() method on each media type:

    public abstract class Base_Collection
    {
    
        public int id = 0;
        public string title = "";
    
        public Base_Collection() {   }
    
        public abstract void Consume();
    
    }
    
    public class Book : Base_Collection
    {
        public Book() {   }
    
        public override void Consume()
        {
            //do book reading stuff in here
        }
    }
    
    public class Movie : Base_Collection
    {
        public Movie() {   }
    
        public override void Consume()
        {
            //do movie watching stuff in here
        }
    }
    
    
    
    public class SomeOtherClass
    {
        public void SomeMethod(Base_Collection mediaItem)
        {
            //note that the book/movie/whatever was passed as the base type
            mediaItem.Consume();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created the following class: using System.Windows; using System.Windows.Input; using MyUtils.MyArgParser; namespace MyUtils.MyImplement
I have a XSD I generated using xsd.exe and then created a class off
I have a window I've created using UiBinder. I'm followings Sencha's HelloWorldUiBinder example and
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
I'm using Sass (.scss) for my current project. Following example: HTML <div class=container desc>
I am trying to create dynamic menus from the database using the following example
I have created the following example to illustrate a problem I have with a
I have the following idea: Business object implemented as interface or abstract class with
So I have the following example tables: Table A: id name type hat_size created
Using Visual Studio's O/R Designer, I have created a number of classes the inherit

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.