What's the difference between declaring a final static variable on Superclass doing so on the extendedClass?

Oak

I'm trying to create Objects. My code is something akin to:

public abstract class Fruit {
private static final Colour DEFAULT_APPLE_COLOUR = red; //let's pretend this is possible

 public static Fruit newInstance(char type){
   Colour myColour = DEFAULT_APPLE_COLOUR;

         switch(type){
            case ('a'): return new Apple(myColour);
 //and onward, until default, etc.

then I have

 public class Apple extends Fruit {
  private final Colour c;

  public Apple(Colour c){
   this.c = c;
  }
}

Now, I established that DEFAULT_APPLE_COLOUR is red, and then use it to create a new Apple. This works just fine. Plus if I want to have it be another colour, I can just make myColour become yellow if I want to, while still being able to call for a default value easily.

However, what the difference between delcaring DEFAULT_APPLE_COLOUR in class Fruit and doing so directly on class Apple, something akin to:

 public class Apple extends Fruit {
  private final Colour c;
  private static final Colour DEFAULT_APPLE_COLOUR = red;

   public Apple(Colour c){
    this.c = c;
  }
   public Apple Apple(){
    return new Apple(DEFAULT_APPLE_COLOUR);
   }
}

In the latter, am I essentially creating several objects , thus creating the same DEFAULT_APPLE_COLOUR variable as many times as I create Apples, or since it's static it's the same variable being applied to all Objects Apple? What exactly is the difference/best way to go about it ?

Sergii Bishyr

I think it's not the best idea for your Fruit class to know details about child class implementations. Image the you have dozens of Fruits, each with some default parameters. In this case you have to hold all of these constants inside of the Fruit class. Moreover, I don't think that you need this constant at all. The default constructor will do the job.

public class Apple extends Fruit {
  private final Colour c;

  public Apple(Colour c) {
      this.c = c;
  }

  public Apple() {
      this(Colour.RED);
  }
}

And the factory method will look like this:

public static Fruit newInstance(char type){
    ....
    switch(type){
        case ('a'): return new Apple();
}

You encapsulate the default behavior to the Fruit implementation, and you still have a way to change this behavior using the main constructor.

What about your factory method, I wouldn't have it inside the Fruit class for the same reason: you parent class depends on child classes and it breaks the Open/closed principle. My suggestion is to create a FruitFactory that will deal with instantiating of your fruits.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the difference between declaring a final variable in an Activity and instantiating it on the onCreate method?

From Dev

Declaring variable final and static

From Dev

What's the difference between a static member variable and a namespace variable?

From Dev

In PHP, what is the difference between "final static" and "const"?

From Dev

what's the difference in position declaring variable in xilinx?

From Dev

Difference between declaring variable

From Dev

Anonymous functions - what's the difference between declaring a global variable and use in php?

From Dev

What's difference between enums and final variables?

From Dev

What is the difference between declaring a variable directly and using the keyword new?

From Dev

What is the difference between using var and not when declaring a variable?

From Dev

Reasons for declaring a private static final variable

From Dev

What is the difference between a static const and constexpr variable?

From Java

What’s the difference between "Array()" and "[]" while declaring a JavaScript array?

From Dev

What’s the difference between “{}” and “[]” while declaring a JavaScript array?

From Dev

What's the difference between declaring a module in TypeScript with quotes vs without?

From Dev

What's the difference between declaring variables inside function and outside functions

From Dev

What is the difference between final, const and static variables in java

From Dev

What's the difference between doing cat and between piping only

From Dev

changing final variables through reflection, why difference between static and non-static final variable

From Dev

What's the difference between "$(variable)" and "$(VARIABLE)"

From Dev

What's the difference between "$(variable)" and "$(VARIABLE)"

From Dev

What is the implementation difference between static variable and static field?

From Dev

Doing an asynchronous HTTP request - what's the difference between these two?

From Dev

What is the difference between static reference variable and instance reference variable in initiating?

From Dev

What's the point of declaring an object as "final"?

From Dev

What's difference between static and non-static resources?

From Dev

What's the difference between declaration 'static const' and 'const static'

From Dev

What's is the difference between a static and non-static annotation?

From Dev

What's the difference between declaration 'static const' and 'const static'

Related Related

  1. 1

    What is the difference between declaring a final variable in an Activity and instantiating it on the onCreate method?

  2. 2

    Declaring variable final and static

  3. 3

    What's the difference between a static member variable and a namespace variable?

  4. 4

    In PHP, what is the difference between "final static" and "const"?

  5. 5

    what's the difference in position declaring variable in xilinx?

  6. 6

    Difference between declaring variable

  7. 7

    Anonymous functions - what's the difference between declaring a global variable and use in php?

  8. 8

    What's difference between enums and final variables?

  9. 9

    What is the difference between declaring a variable directly and using the keyword new?

  10. 10

    What is the difference between using var and not when declaring a variable?

  11. 11

    Reasons for declaring a private static final variable

  12. 12

    What is the difference between a static const and constexpr variable?

  13. 13

    What’s the difference between "Array()" and "[]" while declaring a JavaScript array?

  14. 14

    What’s the difference between “{}” and “[]” while declaring a JavaScript array?

  15. 15

    What's the difference between declaring a module in TypeScript with quotes vs without?

  16. 16

    What's the difference between declaring variables inside function and outside functions

  17. 17

    What is the difference between final, const and static variables in java

  18. 18

    What's the difference between doing cat and between piping only

  19. 19

    changing final variables through reflection, why difference between static and non-static final variable

  20. 20

    What's the difference between "$(variable)" and "$(VARIABLE)"

  21. 21

    What's the difference between "$(variable)" and "$(VARIABLE)"

  22. 22

    What is the implementation difference between static variable and static field?

  23. 23

    Doing an asynchronous HTTP request - what's the difference between these two?

  24. 24

    What is the difference between static reference variable and instance reference variable in initiating?

  25. 25

    What's the point of declaring an object as "final"?

  26. 26

    What's difference between static and non-static resources?

  27. 27

    What's the difference between declaration 'static const' and 'const static'

  28. 28

    What's is the difference between a static and non-static annotation?

  29. 29

    What's the difference between declaration 'static const' and 'const static'

HotTag

Archive