0

Is it possible to set abstract class variable with default value of class which extends abstract class? I mean if I have abstract class Atrakcja:

public abstract class Atrakcja implements ElementWycieczki{
    private double time;
    public Atrakcja(double time)
    {
        this.time = time;
    }
}

I want to set time variable by default value from

public class DrewnianaCerkiew extends Atrakcja{
    private double time = 0.5;
    public DrewnianaCerkiew(){
            ...
    }
}

I know that if i would like to put parameter to constructor it would be easy, just put parameter to DrewnianaCerkiew constructor, then super(param) and it would set time. But in some classes which extends Atrakcja (abstract class) want to set default time.

One of solution what I want is to do is just: super(0.5);, but is there another way to make default value for class? It would be better if I can set default value to variable of class e.g. double time = 0.5; then super(time) but it doesn't work.

7
  • 2
    Did you try it? What problem are you having? It should work. Commented Mar 28, 2021 at 15:19
  • @markspace you mean private double time = 0.5; from DrewnianaCerkiew class sets abstract class Atrakcja time variable by defeault? Commented Mar 28, 2021 at 15:20
  • 1
    "It would be better if i can set default value to variable of class e.g. double time = 0.5; then super(time)" ... no it wouldn't. The only thing you would achieve there is assuming that both time are "connected" and then get confused when methods inside Atrakcja work with a different value than methods inside DrewnianaCerkiew. Commented Mar 28, 2021 at 15:34
  • 1
    What you are trying to do can technically be done. Just replace private double time = 0.5; with { time = 0.5; } and change the definition of time in the base class to be protected instead of private. With this said, you should use the techniques provided in the answers to this question. Commented Mar 28, 2021 at 16:10
  • 1
    "One of solution what I want is to do is just: super(0.5);, but is there another way to make default value for class?" what is the problem with that solution? It looks clear, especially in IDEs which can show names of variables for which we provide values, so you will see it as something like super(time: 0.5). Commented Mar 28, 2021 at 16:13

3 Answers 3

4

It is only possible if you assign the variable in the base class' constructor:

public abstract class Atrakcja implements ElementWycieczki {
    private double time;
    public Atrakcja(double time) {
        this.time = time;
    }
}

public class DrewnianaCerkiew extends Atrakcja {
    public DrewnianaCerkiew() {
            super(0.5d);
    }
}

Adding a new field time to class DrewnianaCerkiew creates a new member which is not connected in any way to the field in the base class. Don't do it, it is confusing.

If all you are looking for is having all "defaults" at the top of the class definition (and not inlined in the super constructor call), you can extract them to a constant:

public class DrewnianaCerkiew extends Atrakcja {
    private static final double DEFAULT_TIME = 0.5d;
    public DrewnianaCerkiew() {
            super(DEFAULT_TIME);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

okey, thanks, I was wondering if I can keep a default value in a variable, then do something like this: public class DrewnianaCerkiew extends Atrakcja { private time = 0.5; public DrewnianaCerkiew() { super(time); } but now i know i cant
@Kawson To be fair you can declare in subclass field like private static final double DEFAULT_TIME = 0.5; (static is important) and use it like super(DEFAULT_TIME); but I am not sure if that would make your file easier.
@Pshemo yes, I was just about to add that to my answer. I'm not sure if it makes life any easier
@Pshemo that's what i was looking for! Just to know that i can do smth like that, for me it is more clear sometimes than put "raw" value to super()
2

Expanding on "knittl"s answer:

Your class DrewnianaCerkiew can have two constructors:

  • one that uses the default value (0.5)
  • another one that allows you to pass the desired value

public abstract class Atrakcja implements ElementWycieczki {
    private double time;
    public Atrakcja(double time) {
        this.time = time;
    }
}

public class DrewnianaCerkiew extends Atrakcja {
    public DrewnianaCerkiew() {
            this(0.5);
    }
    public DrewnianaCerkiew(double time) {
            super(time);
    }
}

Comments

-1

I will interpret your question as, "How do you use a default value of a field for some child classes but not others?"

Please see this article on constructors: you can define multiple constructors in the parent class.

Then, for child classes which use the "default time", simply call the default constructor (i.e. super())

public class Atrakcja {
    protected double time;
    public double DEFAULT_TIME = 52;
    public Atrakcja(double time)
    {
        this.time = time;
    }

    public Atrakcja()
    {
        this.time = DEFAULT_TIME;
    }

    public static void main(String[] args) {
        DrewnianaCerkiew2 dc2 = new DrewnianaCerkiew2();
        dc2.printTime();
    }
}

Simply call super() in the child class:

public class DrewnianaCerkiew2 extends Atrakcja {
    public DrewnianaCerkiew2(){
        super();
    }
    public void printTime(){
        System.out.println(this.time);
    }
}

Output is 52.0

3 Comments

Based on "But in some classes which extends Atrakcja (abstract class) want to set default time." OP may want to have different default value for each subclass.
The point of the question was what to do in the subclass, and there were already two answers that addressed this. Your answer actually removes the whole point of the question. Where did the 0.5 in the OP's subclass go? Getting that value into the base class was the whole point of the question, and you've removed that. All of the stuff you added to the base class is irrelevant and just confuses the problem. - Any correct answer to this question should not touch the base class.
I had misinterpreted "some classes which extends Atrakcja (abstract class) want to set default time" as those classes needing to set the global default time shared among all subclasses. This is a way to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.