5

Kind of a noob question, this, but I cannot figure it out.

This is animal.java. I want it to be a superclass for all animal subclasses. It's in the same package as all the subclasses.

public class Animal {
    protected static String call = "Animals make noises, but do not have a default noise, so we're just printing this instead.";
        public static void sound()
        {
            System.out.println(call);
        }
}

This is cow.java

class Cow extends Animal {
    call = "moo";
}

Evidently, this does not run. But I want to be able to run Cow.sound() and have the output read "moo". I also want to be able to create more classes that override the 'call' with their own string. What should I be doing instead?

cjhines
  • 1,148
  • 3
  • 16
  • 32
Skeleton
  • 107
  • 1
  • 6
  • Use the class variable straightaway by classname.variable. Because static methods and variable cannot be overridden. So if you want to change the variable you have to call Animal.call= – Shriram Oct 22 '15 at 11:04
  • try something like `void sound(String call){ ----- }` – Uppicharla Oct 22 '15 at 11:05
  • Why don't you simply override the sound method instead of trying to redefine your static attribute? – christophetd Oct 22 '15 at 11:08
  • Possible duplicate of [Is there a way to override class variables in Java?](http://stackoverflow.com/questions/685300/is-there-a-way-to-override-class-variables-in-java) – Tarik Oct 22 '15 at 11:09
  • Can you add the error message you are getting? Why do you use `static` for `call` variable and for `sound()` method? – Amir Oct 22 '15 at 11:15
  • Ideally ,Your animal Class should be abstract since no one could create instance of it ,and your Concrete classes will be real object – rupesh_padhye Oct 22 '15 at 11:18

2 Answers2

6

You can't override instance variables. You can only override methods. You can override the sound method (once you change it to an instance method, since static methods can't be overridden), or you can override a method that sound will call (for example getSound()). Then each animal can returns its own sound :

public class Animal {
    static String call = "Animals make noises, but do not have a default noise, so we're just printing this instead.";
    public void sound()
    {
        System.out.println(getSound ());
    }

    public String getSound ()
    {
        return call;
    }
}

class Cow extends Animal {
    @Override
    public String getSound ()
    {
        return "moo";
    }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • ... it seems that using @Override doesn't work, but it works fine if I do not. As a followup, can this be done with static methods? Since the objects stay the same every time, there's no point to instansiating them, right? It just uses more memory... right? – Skeleton Oct 24 '15 at 19:57
0

Variables are never overriden, so sub class variable replacing supercall variable will not be possible. Another option was to override the method but then its static, static also cannot be overridden.

So with current setup its not possible unless you look to override non static methods.

Lokesh
  • 7,810
  • 6
  • 48
  • 78