Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static abstract class

I need a way to create a static class where some constants can be case specific, but hard-coded.

What I really want to do is have a class where several constants are provided when the class is extended - I want the 'constants' hard-coded. I figured I will make the some abstract properties and define the get { return constant; } when extending the class.

I know that is not possible, so now I am facing two options and am wondering what would be best and why (if there are options I'm missing please let me know!)

  1. Create a static class with nullable fields and throw an exception if the fields are null when the static method is called.
  2. Give up the static class. Have a non-static class with abstract properties and create an instance of the object wherever I need it even though all the functionality really is static.

I know this might be subjective and case-dependant, however I am going around in circles when thinking about this and could really do with some external input. That plus I hope there might be away of doing what I want and I'm just thinking about this wrong.

Update: Code: I will try to write some code that describes what I'd like to accomplish. I know this code can't work!

Imagine that the abstract class Calculation is in a dll, used by many projects. The functionality is the same for all of them, just the Constant varies from project to project.

public abstract static class Calculation
{
    private abstract int Constant { get; }    //The constant is unknown at this time

    public static int Calculate(int inputValue)
    {
        return inputValue * Constant;
    }
}

The class Calc is defined in a separate project where the functionality is needed and the Constant is known.

public static class Calc : Calculation
{
    private override int Constant { get { return 2; }
}

...

static class Program
{
    [STAThread]
    static void Main()
    {
        //At some point:
        int result = Calc.Calculate(6);
    }
}

I suppose the simplest way would be to create a non-static class and create an instance, however I fear having several instances of the class could be expensive and would like to prevent that if possible.

I can't see how I could write this as a singleton pattern without writing it again in each project - having only the Nested class in the dll. That doesn't prevent the implementor to just create an ordinary class and is likely to restart the debate for every project where the code is used.

Update #2 : What I ment with option one is this:

Class in a dll:

public static class Calculation 
{
    public int? Constant {get; set;}

    public static int Calculate(int inputValue)
    {
        if (Constant == null)
            throw new ArgumentNullException();

        return inputValue * (int)Constant;
    }
}

Usage of the function in a seperate project:

static class Program
{
    [STAThread]
    static void Main()
    {
        //At some point:
        Calculation.Constant = 2;
        int result = Calc.Calculate(6);
    }
}

Option one is very simple and elegant, what bothers me about it that nothing forces the implementor to set the Constant. I fear an (admittedly unlikely) scenario where an obscure corner case will cause the property to not be set and for the code to fail (and Constant beeing the last suspect)...

like image 258
David Božjak Avatar asked Sep 13 '11 08:09

David Božjak


People also ask

What is static class and abstract class?

The 'abstract' keyword is used to create abstract classes. Abstract Method: An abstract method is a method that can only be declared and not defined. It is defined in the class that inherits from this class. Static Method: A static method of any class is a method that is created only once.

Can we have static abstract class in Java?

In Java, a static method cannot be abstract. Doing so will cause compilation errors.

Can we use static in abstract class?

Yes, abstract class can have Static Methods. The reason for this is Static methods do not work on the instance of the class, they are directly associated with the class itself.

What is static abstract?

The definition of an abstract method is "A method that is declared but not implemented", which means it doesn't return anything itself. The definition of a static method is "A method that returns the same value for the same parameters regardless of the instance on which it is called".


1 Answers

You could make non-static classes that follow singleton, ensuring only one instance of the object ever to exist. I guess that could be the next best thing.

like image 192
Joachim VR Avatar answered Sep 28 '22 06:09

Joachim VR