Skip to content
Advertisement

Two classes and one method with different implementation for each of them

I’m just starting to learn OOP in Java, so this might be a dumb question. The Cat class extends Animal, they have a constructor, a static array, a method that fills the array, and a method that creates an object of the Moving class. On this created object of class Moving, the walk() method can be invoked. Question: how to write different behavior in the walk () method, depending on how the object was created (who was the creator Animal or Cat)? I was thinking of writing a separate efficiency() method to use in the walk() method, and put it in the Animal class and override this method in the Cat class. But in the walk() method, I can only use the static method of the Animal class and I can’t override it for the Cat class.

If in Main I create Animal noName = new Cat(), then the program should call Cat.efficiency(). If I create Animal noName = new Animal(), then the Animal.efficiency() method must be called. Something like this, I think. Is it possible?

JavaScript

Advertisement

Answer

Overriding the efficiency method

If you want to be able to override your efficiency method in a subclass (in the Cat class), you need to make it an instance method instead of a static method.

So you would remove the static keyword from the efficiency method in the Animal class

JavaScript

You would do the same in the Cat class but here you would also add the @Override annotation

JavaScript

Now the compiler will start complaining that the Moving class cannot access efficiency because the method cannot be called as a static method anymore. Now the Moving class needs an instance of the Animal class to be able to call the efficiency method.

JavaScript

And now the compiler will complain that new Moving() is not a constructor anymore, so you have to do another change

JavaScript

Potential issue: Static storage for instance related data?

Using a static array animalArray (to which all Animal instances are adding values) to store data that belongs to each individual Animal instance won’t produce correct results when calculating total steps for each Animal.

With your current example code it will work as you probably expect it to, but as soon as you will create another Animal instance (or another Cat instance) you will probably get unexpected results.

If your goal is to hold/store state that belongs to an instance, you want to use a non-static field for that. You would do the same as shown above for the efficiency method. Remove the static keyword from the animalArray field and change the code where it is accessed to use instance access instead of static access.

It seems the array is storing steps, so it would be less confusing if it would be called stepsArray instead of animalArray.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement