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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:12:03+00:00 2026-06-13T05:12:03+00:00

The title may be unclear, but please look at the pattern below public abstract

  • 0

The title may be unclear, but please look at the pattern below

public abstract class Animal
{

    public abstract Dog GetDog { get; }

    public abstract Cat GetCat { get; }

}

public class Dog : Animal
{

    public override Dog GetDog {
        get { return this; }
    }

    public override Cat GetCat {
        get { return null; }
    }

}

Is this considered a bad practice to have properties in the base class, return derived types. Or should I do something like

public abstract AnimalTypeEnum AnimalType { get; }

EDIT: Based on the comments, I guess I should be more clear on what I am trying to achieve. A new instance of the Dog or Cat class would be created by a separate function based on certain criteria and would then return Animal type to the caller. The calling method would check the type of the returned instance and use it accordingly.

public Animal CreateAnimal(string path)
{

    //Process the document in path and create either a new instance of dog or cat

    Dog dg = new Dog();

    return dg;

}
  • 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-06-13T05:12:04+00:00Added an answer on June 13, 2026 at 5:12 am

    The calling method would check the type of the returned instance and use it accordingly.

    There’s your problem. It’s code smell to need to do that. You should be able to treat whatever it is as just an object, rather than treating dogs and cats differently.

    If you need to display the content of either animal, then override the ToString method on both classes and just call ToString on animal. If you need to know the name of the dog or cat then add a Name property to Animal. If at all possible you should be using polymorphism here so that whatever is using the object treads it as just an Animal and simply involves different things happening as a result of different implementations of the same method.

    If you really, really do need to know whether the Animal is a Dog or a Cat then you can use the is or as operators; you don’t need to add all of the code that you’ve shown in the OP.

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

Sidebar

Related Questions

The title may have been confusing, but please let me explain: Currently when I
Title may be confusing, but here's explanation with code. Based on some conditions, I
The title may be a bit ambiguous, but I couldn't think of a better
The title may be a little confusing, but I don't know how else to
The title may sound confusing but it actually isn't, I just didn't know how
The title may sound a bit odd but I wanted to keep the title
The title may seem a little weird but what I find around the internet
My Title may be slightly off but here is what I am trying to
(The title may not be proper but I try my best to phrase it)
I understand the title may be a little vague, but, hopefully, I can explain

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.