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

  • Home
  • SEARCH
  • 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 6387891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:12:36+00:00 2026-05-25T03:12:36+00:00

I am trying to create a static method that would return an instance of

  • 0

I am trying to create a static method that would return an instance of the class, something like:

class A {
   public static A getInstance() {
      return new A();
   }
}

The problem I am having is that if I have a subclass B derived from A, I would like B.getInstance() to return an instance of B, and not A. In PHP world, you could use a keyword “self” to reference to your own type, so your getInstance() would look like:

public static function getInstance() {
   return new self();
}

What’s the best way to go about 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-25T03:12:37+00:00Added an answer on May 25, 2026 at 3:12 am

    You can hide and so in a way override the getInstance method in the inheriting type like this:

    class B : A {
        public static new B getInstance() {
            return new B();
        }
    }
    

    The new keyword specifies that the hiding of the inherited method was intentional, otherwise you get a compiler warning.

    Complete example:

    using System;
    
    class A {
        public static A getInstance() { return new A(); }
        public void PrintSelf() {Console.WriteLine(this.GetType().Name);}
    }
    
    class B : A {   
        public static new B getInstance() {
            return new B();
        }
    }
    
    public class MyClass {
        public static void Main() {
            A a = A.getInstance();
            B b = B.getInstance();
            a.PrintSelf();
            b.PrintSelf();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to create a Python class decorator (*) that would be able to
I'm trying to create a generic method that will return a predicate to find
Trying to create my first iPhone app that would play back audio. When I
I am trying to create an expression that invokes an internal method, the internal
I'm trying to create a generic extension method, that works on typed data tables
Trying to construct a helper class that will return an arraylist, but I'm getting
C#, .net 3.5 I am trying to create a base class that has a
I am trying to create a class that lets me call methods with a
I would like to create a new System.Windows.Xps.Packaging.XpsDocument object from byte array, as I
I'm trying to create a utility method that will accept two arrays as parameters,

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.