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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:10:47+00:00 2026-05-16T22:10:47+00:00

Consider the following problem: You have a class ‘A’ that serves as a base

  • 0

Consider the following problem:

You have a class ‘A’ that serves as a base class for a lot of other similar classes, eg. a class called B.

The class A is useful in it self (and already used all over the place) and is hence not abstract.

The crux is that you now wish to enforce a new protocol requiring all classes inheriting from A (including A itself) to implement a method ‘func()’. If a sub class forgets to implement func() there should be a compiler error.

class A {
  A func() { ret new A(...) }
}

class B : A {
  A func() { ret new B(...) }
}

class C : A {
  // Should give error : no func()
}

Is the problem clear? I can’t make A::func() abstract since I want to be able to keep using A as a concrete class. I can’t make it virtual since that would cause sub classes to fall back on the super class (and not give compiler errors).

The only thing that comes close to a solution is creating a new abstract class A* and have all custom types inherit from that and replace all current usages of A as a concrete class with a new class ‘DefaultA’ that inherits from A*. This seems messy. Please tell me there is some other way to do this?

  • 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-16T22:10:48+00:00Added an answer on May 16, 2026 at 10:10 pm

    If that is what you need to do, then yes.

    In my opinion though, you may wish to reconsider this requirement. I have often implemented similar frameworks, where forcing implementations in sub-classes was a clever trick. What I then found was that as a consumer of these frameworks I would duplicate code or default to some inane implementation (like the empty method). Neither of which is ideal, as they add clutter and noise, and reduce maintainability.

    Additionally, if the application of this pattern is as a self-factory (ie your example has sub-classes returning instances of themselves), then you may wish to try something different, like a proper factory pattern. Er, and by “try” I mean leverage an Inversion of Control Container, like Castle Windsor, Ninject, Unity.

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

Sidebar

Related Questions

Consider the following. You have a class that you want to serialize with XmlSerializer
I have a problem that I can't seem to resolve. I'm sure it's simple,
We have a production system that uses a lot of Serialization. What be basically
Please, consider the following (I'm sorry for the amount of code; but this is
I stumbled upon an interesting error that I've never seen before, and can't explain
I recently started to think of this problem and I can't find the answer.
I develop a web application for Tomcat 6, and I'm face to face with
Is there a (practical) way to by-pass the normal (virtual) constructor calling order? Example:
I'm having a remoting issue in my application. Since the architecture is quite complex,

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.