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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:07:27+00:00 2026-05-13T07:07:27+00:00

I have the following setup (simplified, obviously): An abstract class, with an A object

  • 0

I have the following setup (simplified, obviously):

An abstract class, with an A object and an abstract method

abstract public class X
{
    protected A myA;
    abstract public int MethodForX();
}

Two inheriting classes, each of which override the method. However instead of using an A object they use a B and C object, each of which inherit from A.

public class Y : X
{
    public Y()
    {
        myA = new myB(); //B inherits from A
    }
    override public int MethodForX()
    {
        return (myA as myB).MethodForB(1);
    }
}

public class Z : X
{
    public Z()
    {
        myA = new myC(); //C inherits from A
    }
    override public int MethodForX()
    {
        return (myA as myC).MethodForC(1, 2);
    }
}

I don’t have direct control over A, B and C. (Specifically B and C are two different table adapters, A is their common ancestor, and the MethodForB/C is a GetData method, with different arguments for each.)

I don’t like using “as” every time I want to access the field. I’d like for Y and Z to know that the field is of their required class, but restrict that class to an inheritor of the class in X.

Is there a way of doing that, or not, or am I trying to do something horrendous?

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

    You could try generics, although it isn’t pretty:

    abstract public class X<T> where T : A
    {
        protected T myA;
        abstract public int MethodForX();
    }
    
    public class Y : X<B>
    {
        public Y()
        {
            myA = new B(); //B inherits from A
        }
        override public int MethodForX()
        {
            return myA.MethodForB(1);
        }
    }
    
    public class Z : X<C>
    {
        public Z()
        {
            myA = new C(); //C inherits from A
        }
        override public int MethodForX()
        {
            return myA.MethodForC(1, 2);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following setup. method and method1 both are public methods of class Student.
I have the following setup in one module: class A(object): # stuff class B(object):
I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #...
I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have the following setup, and the empty view text doesn't show up... protected
I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
I have following setup A -> B -> C A-Mapping: <hibernate-mapping> <class name=db.base.A table=A>
i have following model setup class Category < ActiveRecord::Base has_ancestry :cache_depth => true, :depth_cache_column
Using Ninject 2.2, I have the following failing test (simplified): public interface IGenericView<T> {
I have following setup: .calender-month{ float:left; } .buttondiv{ float:left; } <div class=buttondiv><button id=backbtn>back</button> <div

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.