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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:54:57+00:00 2026-05-11T20:54:57+00:00

Lets say I want to design a abstract system for counting sections in a

  • 0

Lets say I want to design a abstract system for counting sections in a document. I designed two classes, Document and Section, the document has a list of sections and a method to count them.

public abstract class Document {
  List<Section> sections;

  public void addSection(Section section) { 
    sections.Add(section);
  }
  public int sectionCount() { 
    return sections.count;
  } 
}
public abstract class Section {
  public string Text;
}

Now, I want to be able to use this code in multipe scenarios. For example, I have Books with Chapters. The Book would be a subclass of Document, and Chapter a subclass of Section. Both classes will contain extra fields and functionality, unrelated to the counting of sections.

The problem I stumble upon now is that because Document contains sections, and not Chapters, the added functionality of Chapter is useless to me, it can only added as a section to Book.

I was reading about downcasting, but really think this is not the right way to go. I’m thinking maybe I took the wrong approach altogether.

My question comes to this: How do I design such an abstract system, that can be reused by subclassed objects, and is this the way to go?

  • 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-11T20:54:57+00:00Added an answer on May 11, 2026 at 8:54 pm

    You need generics:

    public abstract class Document<T> where T : Section
    
    public abstract class Section
    
    public class Book : Document<Chapter>
    
    public class Chapter : Section
    

    You might also want to make a section know what kind of document it can be part of. Unfortunately that becomes a lot more complicated:

    public abstract class Document<TDocument, TSection>
        where TDocument : Document<TDocument, TSection>
        where TSection : Section<TDocument, TSection>
    
    public abstract class Section<TDocument, TSection>
        where TDocument : Document<TDocument, TSection>
        where TSection : Section<TDocument, TSection>
    
    public class Book : Document<Book, Chapter>
    
    public class Chapter : Section<Book, Chapter>
    

    I’ve had to do this in Protocol Buffers, and it’s messy – but it does allow you to reference both ways in a strongly-typed way. I’d go for the first version if you can get away with it.

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

Sidebar

Related Questions

Let's say I want to design a REST store used to manage a list.
Lets say I want to make a class myClass with two slots A and
Lets say I want to make few classes to determine behaviour of agents. The
So this is a newbie rails design question. Lets say I want some of
I have an Interface lets say ISendOut which I've inherited two different classes from
lets say you want to get all the columns of a table, but exclude
Lets say I want to define 2 styles. .color_red { color: Red; } .banner
Lets say we want a library of javascript-based pieces of functionality (I'm thinking jquery):
Lets say I want to simulate a particle state, which can be normal (0)
Lets say I want to sort some items and need to sort them by

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.