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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:05:32+00:00 2026-05-28T03:05:32+00:00

I have the following classes: class Alpha { } class Beta : Alpha {

  • 0

I have the following classes:

class Alpha
{
}
class Beta : Alpha
{
}
class Gamma : Beta
{
}
class Epsilon : Beta
{
}

And I have a couple methods where I take them as parameters:

void Receiver(Gamma gamma);
void Receiver(Epsilon epsilon);
void Receiver(Beta beta);
void Receiver(Alpha alpha);

I require something a bit unusual here.
I want to be able to pass Alpha objects to all methods, but I don’t want methods like void Receiver(Beta beta); being able to receive objects of types that inherit from Beta (that is, I want to, at worst, raise an Exception if a Gamma or Epsilon object is passed.

How can I best implement this in such a way that is generic enough?

I was thinking for the first part, I should have a method that allows for upcasting, such as

class Alpha
{
    public T Upcast<T>() where T : Alpha
    {
        // somehow return a T
    }
}

The issues here are that I want this to work on all levels, for instance I want being able to “upcast” Beta too.

The second part should be easier, as I would just throw on objects being passed that are above the type I require.

so something like this should suffice:

void Receiver(Epsilon epsilon)
{
    // if epsilon inherits from Epsilon, throw.
}

Can you help me with the first part of my problem? 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-28T03:05:32+00:00Added an answer on May 28, 2026 at 3:05 am

    You seem to be struggling with code that can deal with a given type, but it cannot be trusted to deal properly with subtypes. This is generally speaking a violation of many OOP principles, one of them being LSP, or Liskov Substitution Principle.

    You might be in a place where you could consider the visitor pattern as a means of overcoming this problem. It’s very close to what you already have, with some minor modification, which is good! It’s nice when patterns emerge from your code, rather than being forced upon it.

    Your void Receiver methods are essentially already visitor implementations, so let’s rename them and create an interface.

    interface IVisitor
    {
        void Visit(Gamma gamma);
        void Visit(Epsilon epsilon);
        void Visit(Beta beta);
        void Visit(Alpha alpha);
    }
    

    Your class that is handling the processing of each element type simply needs to implement that interface.

    class Visitor : IVisitor
    {
        public void Visit(Gamma gamma) { Console.WriteLine("Visiting Gamma object"); }
        public void Visit(Epsilon epsilon) { Console.WriteLine("Visiting Epsilon object"); }
        public void Visit(Beta beta) { Console.WriteLine("Visiting Beta object"); }
        public void Visit(Alpha alpha) { Console.WriteLine("Visiting Alpha object"); }
    }
    

    Then have each element type also implement a simple and common interface

    interface IElement { void Accept(IVisitor visitor); }
    
    class Alpha : IElement 
    {
        public virtual void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }
    } 
    
    class Beta : Alpha 
    {
        public override void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }
    }
    
    // etc.  
    

    (Strictly speaking, the interfaces aren’t necessary, it’s just good form.)

    And there you go, a method of double dispatch where an element invokes a visitor method specific to its own type, even though you might have a reference to it via a base type.

    Alpha alpha = new Beta();
    IVisitor visitor = new Visitor();
    alpha.Accept(visitor);
    

    If you have implemented it properly, you should see “Visiting Beta object” on the screen (or, in your terms, you should see the desired “Receiver” behavior executed.)


    Another option you might consider is simply making Receiver a virtual method on your base class and let the derived classes override it, and have the behaviors inside the classes, if that makes sense for your hierarchy. If the behaviors need to be external, you could also consider a factory approach that knows how to create a specific processor for a given class. There are many options you have that do not involve your program going nuts if a subtype is passed to a method expecting the base.


    I require something a bit unusual here. I want to be able to pass
    Alpha objects to all methods, but I don’t want methods like void
    Receiver(Beta beta); being able to receive objects of types that
    inherit from Beta (that is, I want to, at worst, raise an Exception if
    a Gamma or Epsilon object is passed.

    This is very strange indeed. And I’m not sure you actually want that. You seem to have the inheritance hierarchy flipped on its head, where super types can be substituted for derived types and not the other way around, and you should really rethink what you are trying to do.

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

Sidebar

Related Questions

I have following classes. class A { public: void fun(); } class B: public
I have the following classes: class A { public: virtual void f() {} };
I have the following classes: class A { public: virtual void myfunc(unsigned char c,
I have the following classes class Parent { virtual void doStuff() = 0; };
I have the following classes: class Vigil < ActiveRecord::Base after_update :do_something_cool private def do_something_cool
Assume I have the following classes class Genre { static hasMany=[author:Author] } class Author{
I have the following classes: public class Person { public String FirstName { set;
I have the following existing classes: class Gaussian { public: virtual Vector get_mean() =
I have the following c# classes: class A : Object { foo() {} }
I have the following classes: Public Class Email Private Shared ReadOnly EMAIL_REGEX = \b[a-zA-Z]+[a-zA-Z0-9._+-]+@

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.