-1

This is my code and i am trying to pass the parameter from main to cat class but its saying no constructor cant figure out what to do a little help would be appreciated.

public class Cat extends Animal implements Pet {

    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Cat(String name, int legs) {
        super(4);
        this.name = name;
    }

    public Cat() {
        this("Fluppy"); //ERROR OVER HERE
    }

    @Override
    public void play() { //THIS METHOD IS OVERRIDDEN FROM PET INTERFACE
        System.out.println(name+"Likes to play with string");
    }

    @Override
    public void eat() {   /*THIS METHOD IS OVERRIDDEN FROM ANIMAL ABSTRACT METHOD.*/
        System.out.println("Cats likes to eat spiders and fish");
    }
}

and the main class

public class PetMain {

    public static void main(String[] args) {

        Animal a;
        Pet p;
        Cat c= new Cat("Tom"); //IM GETTING THE ERROR OVER HERE.
        c.eat();
        c.walk();
        c.play();
    }
}
Nicktar
  • 5,548
  • 1
  • 28
  • 43
codeBeginner
  • 3
  • 1
  • 2

4 Answers4

1

Take a look at your constructors in Cat

public Cat(String name, int legs) { // accept String and int constructor
 super(4);
 this.name = name;
}

public Cat() {  // no argument constructor 
 this("Fluppy"); 
}

There is no matching for new Cat("String")

You can add new constructor

public Cat(String anyThing) {

 }
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

try using the correct constructor which takes two parameters

Cat c= new Cat("Tom", 4); 

and

this("Fluppy", 4);

or make a new constructor for one parameter like

public Cat(String name) {
  this (name, 4);

}

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

First thing when you call this

Cat c= new Cat("Tom");

It expects that you Cat class have a single argument constructor which your class doesnot contain so create a single argument constructor in your Cat class like this

public Cat(String str) {
  // your logic
}

Secondly this("Fluppy"); //ERROR OVER HERE

If you know about constructor chaining then you would not have done this. this() is usually used when you want to call another constructor of the same class from within one constructor in your case you are calling one-parameterized constructor from you default constructor since one-parameterized constructor doesnot exist it is giving you compilation error

SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

You are trying to overload the constructor at:

public Cat() {
    this("Fluppy"); //ERROR OVER HERE
}

but the call made is for the constructor with one String argument. You do not have a constructor with one String argument , So you have an error try to add.

public Cat(String catty) {
        //  initialise
    }
George Rosario
  • 761
  • 11
  • 24