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

  • Home
  • SEARCH
  • 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 6328823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:31:50+00:00 2026-05-24T17:31:50+00:00

I’m refactoring, and have run into a roadblock. Background: I have a base class

  • 0

I’m refactoring, and have run into a roadblock.

Background:

I have a base class and several inherited derived classes. The derived classes don’t always need to have the same properties. If any properties are shared among the derived classes, those properties would live at the base class level (‘Contents‘, for example).

Similarly, GoodDocument below has ‘GoodThings‘ but would not want/need to have ‘BadThings‘.

I want to treat instances of both ‘GoodDocument‘ and ‘BadDocument‘ as type ‘Document‘

public mustinherit class Document
  public property Contents as string
  public sub new()...
end class 

public class GoodDocument
  inherits Document
  public property GoodThings as string
  public sub new()...
end class

public class BadDocument
  inherits Document
  public property BadThings as string
  public sub new()...
end class

The ‘DocumentWriter‘ class will also have several derived classes: (‘GoodDocumentWriter‘ and ‘BadDocumentWriter‘).

I need to pass around the DocumentWriter.Doc as a ‘Document’ to a number of other places in the code. Doc.GoodThings would only be called from within an instance of either ‘GoodDocument’ or ‘GoodDocumentWriter’.

public mustinherit class DocumentWriter
  public property Doc as Document
  public sub new()... 
end class

public class GoodDocumentWriter
  inherits DocumentWriter
  public sub new 
    mybase.Doc = new GoodDocument
  end sub
end class

public class BadDocumentWriter
  inherits DocumentWriter
  public sub new 
    mybase.Doc = new BadDocument
  end sub
end class

Question:

  • Is there a design pattern that allows for derived classes to have members that don’t exist at the base class level?

  • Do all properties have to live at the base class level?

Revised

I was trying to be brief with my initial question and I made the mistake of over simplifying the situation. In short, I did realize that it should be possible to have different properties on each of the derived classes. (I typed that in a tongue-in-cheek manor and didn’t mean to keep it in the final post).

I realize now that the problem that I was experiencing was really symptomatic of a larger issue which needed addressing.

It appears that I was encountering compiler complaints that could be corrected by further refactoring and looser coupling. While others answered the basic question that I posed, Ryan Gross’ example really helped kick start some new ideas.

Thanks!

  • 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-24T17:31:51+00:00Added an answer on May 24, 2026 at 5:31 pm

    What you should do in this case is define the operations that can be performed on instances of Document in an interface. In your case maybe there is a WriteThings operation, so you would have:

    public interface Writeable {
        public sub WriteThings();
    }
    

    Then in your derived classes you would implement the method to utilize the internal data of the class. For example:

    public mustinherit class Document implements Writeable
      public property Contents as string
      public sub new()...
      public sub WriteThings();
    end class 
    
    public class GoodDocument
      inherits Document
      public property GoodThings as string
      public sub new()...
      public sub WriteThings()
         //Do something with GoodThings
      end sub
    end class
    
    public class BadDocument
      inherits Document
      public property BadThings as string
      public sub WriteThings()
         //Do something with BadThings
      end sub
      public sub new()...
    end class
    

    Finally, client code that needs to call WriteThings accesses it through an interface:

    public mustinherit class DocumentWriter
      public property Doc as Writable
      public sub new()... 
      public sub PerformWrite()
        Doc.WriteThings();
      end sub
    end class
    

    It is generally not a good idea to build several parallel class hierarchies. In this case, one DocumentWriter class should be able to write any class that implements Writeable by invoking its WriteThings method.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.