How can I add two variables in a java subclass to pass as one variable to the superclass?

I'm just learning Java, and I have a question. I'm not sure that I'm using the correct terminology, so please correct me if necessary. I'm curious if there is a better way to add the front and back claws to pass them to the Animal class. This works as is, but I'm having trouble searching for what I am trying to do, which is making it hard to figure out if I'm doing it wrong. Here's my subclass that I'm working with.

public class Bear extends Animal // This is a Bear class, it is a subclass of Animal.
{
  private int frontClaws; // This is an instance variable for front claws.
  private int backClaws;  // This is an instance variable for back claws.
  public Bear (String name, int fC, int bC) // This is the Bear constructor.
  {
    super (name, fC + bC); // This passes the name and the sum 
                           // of the claws to the Animal's constructor

    frontClaws = fC;  // not sure why this has to be done, 
                      // and frontClaws + backClaws can't be used directly. Because they are private?
    backClaws = bC;   // Same as confusion as above.
   }
    public void print()  // This calls the print method.
   {
    super.print();    // This calls the print method from 
                      // Animal and prints the args from Animal 
                      // as well as the args from Bear.
    System.out.println("- " + frontClaws + " Front Claws, and " + backClaws + " Back Claws.");
   }
}

Thanks in advance!

tc

Jon Skeet
people
quotationmark

You can't access fields before you call the superclass constructor - basically the very first thing that any constructor does is invoke a superclass constructor. The only thing you get to do is evaluate arguments to call that superclass constructor - in your case, that involves summing fC and bC.

Basically, assuming you need Animal to have the sum of frontClaws and backClaws, this looks like a reasonable implementation.

people

See more on this question at Stackoverflow