Maximilian Conroe
Maximilian Conroe

Reputation: 135

How to avoid redundant code for static methods

Let's assume I have the following classes:

public class Cat {
    private final String noise = "meow";

    pulic static void makeNoise(){
        System.out.println(noise);
    }
}

public class Dog {
    private final String noise = "woof";

    pulic static void makeNoise(){
        System.out.println(noise);
    }
}

As you can see, these two classes share pretty much the same code. To remove redundant code, I'd create a parent class like this:

public abstract class Animal {
    protected final String noise;

    public Animal(String noise) {
        this.noise = noise;
    }      

    public void makeNoise() {
        System.out.println(noise);
    }
}

public class Dog extends Animal{
    public Dog(){
        super("woof");
    }
}

Now unfortunately I'm running into a two problems:

  1. Since make noise of animal can't be static anymore, as the constants will have to be assigned through the constructor of animal, you will need to create a cat or dog to get the noise of that animal. Even though it is a constant.
  2. The method makeNoise() need's to work in the class Animal - which doens't have a noise per default.

A possible solution would be something along the line like this: public abstract void makeNoise(); which is neither allowed, nor would it erase the need to copy the code into each and everyone of the children of Animal.

How would you erase the need to have redundant code in the children of animal while keeping the method makeNoise static?

Upvotes: 0

Views: 1153

Answers (2)

Lucian
Lucian

Reputation: 824

Why would you represent a real world object behavior with a static method? Each animal has it's own behavior so you want to differentiate between them.

 enum AnimalBehavior {
    MEOW, WOOF, ROAR
}

You could use an enum which contains every animal behavior. Also consider the following situation: A wolf keeps howling during a full moon night. He keeps doing it until he gets exhausted. You want to track a bar which indicates the level of energy your wolf has.

private int energy = 100;

public static void wolfHowl() {
    System.out.println(AnimalBehavior.ROAR);
    energy = energy - 10;
}

This won't work technically because you're using static methods.. so keep in mind how you design your stuff since that wolf could actually howl without getting tired until someone gets really pissed off.

Upvotes: 0

David SN
David SN

Reputation: 3519

Static methods in Java can't be overridden in subclasses.

If you define a static method in a subclass with the same signature as the static method in the parent class, the method is not overriding the parent method is hiding it. The methods in the parent and the child class has no relation to each other.

In your example, if method static void makeNoise() exists in Animal and any subclass define the method as well, the subclass is just hiding the makeNoise method in Animal.

In your example, the best you can do with static methods is:

public static void makeNoise(String noise) {
    System.out.println(noise);
}

And invoke the method this way:

Animal.makeNoise(Cat.NOISE); // A constant NOISE is defined in each subclass

If the method makeNoise is non-static, inheritance could be used to use a different noise in each subclass:

public abstract class Animal {
    protected String noise;

    protected Animal(String noise) {
        this.noise = noise;
    }

    public void makeNoise() {
        System.out.println(noise);
    }

}

public class Cat extends Animal{
    public static final String NOISE = "meow";

    public Cat() {
        super(NOISE);
    }
}

Upvotes: 1

Related Questions