-1

I've searched and read my text book for what is probably a basic question but can't seem to figure it out.

I have the following 3 classes. One abstract, one interface and a regular class. In the Cat class I want to call the Animal class constructors within a Cat constructor method but get an error. What am i doing wrong?

package MyPet;
public interface WalkMe {
        public void walk();
}


package MyPet;
public abstract class Animal{ 
    private int legs;
    private String name;

    public abstract String toString();

    public Animal(int legs_in, String name_in){
            setLegs(legs_in);
            setName(name_in);       
                }
    ...removed the rest of code
}

package MyPet;
public class Cat extends Animal implements WalkMe{

    public void walk() {
        System.out.println(" ");
    }

    public String toString() { 
    }

    public Cat(){
        Animal(0, "Unnamed"); //THIS IS WHAT I NEED HELP WITH I am trying to call the animal Constructor
    }

    public static void main(String[] args) {
        Cat[] cats = new Cat[4];
        cast[0] = new Cat();
    }
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Kim
  • 13
  • 2
  • This answered my questions. I was unaware of the "super" keyword so i was unable to search for this specifically. But this helped a lot! – Kim Mar 06 '16 at 19:52

3 Answers3

1

Super class should be refer by the key word super, not with Name of it

 public Cat(){
        super(0, "Unnamed"); 
    }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Abstract constructors aren't meant to be called using their name. In a subclass, to call one, you use super(args);.

So you would change:

public Cat(){
    Animal(0, "Unnamed");
}

to

public Cat(){
    super(0, "Unnamed");
}

which should fix your problem.

No Name
  • 131
  • 1
  • 10
1
public Cat(){
    super(0, "Unnamed");
}
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66