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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:01:12+00:00 2026-05-18T07:01:12+00:00

I have in mind a set of classes which all will need to be

  • 0

I have in mind a set of classes which all will need to be instantiated
with some dependency object during construction. Consider these classes:

class A { A(IDependency dep) {...} }  
class B { A(IDependency dep) {...} }  
class C { A(IDependency dep) {...} }  
class D { A(IDependency dep) {...} }  

I want the DI to take place in the constructor, and obviously I want classes A,B,C,D to inherit from some abstract class or implement the same interface.

Problem: I can’t include the constructor in an interface or an abstract class.

What to do? Inherit from a non-abstract base class?
The base class doesn’t have a real meaning.
Use factory methods? Never really liked them…

Any suggestions?

  • 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-18T07:01:13+00:00Added an answer on May 18, 2026 at 7:01 am

    Of course you can have a ctor in an abstract class.

    public interface IDependency
    {
    }
    
    public abstract class A
    {
        protected IDependency _dep;
    
        protected A(IDependency dep)
        {
            _dep = dep;
        }
    }
    
    public class B : A
    {
        public B(IDependency dep) : base(dep)
    
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a family of classes which i feel needs some encapsulation over the
I'm new to c++ but have set my mind on a specific task that
Here is what I have in mind: 1) Create a service that will run
This is what I have in mind: Can anyone point out some relevant resources?
I have something in my mind which have been bothering me for quite a
I have a project in mind that will require the majority of queries to
I have a project in mind that will offer users their own domain, like
I have 2 derived classes that will be serialized into xml. While the code
Consider the following existing classes which uses MEF to compose Consumer . public interface
i need to set up a IS-A relationship with coredata. I have a Page

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.