0

Completely new to this forum as well as programming in Java. I've seen the other threads about this problem but it hasn't helped me, maybe it should've and I've just missunderstood it due to my so far shitty programmingvocabulary. Anywasy- to the problem;

package animals;

public class Animal {
String name;
public Animal(String string) {
}
public void introduceYourself(){
    System.out.println("Morr. Jag är ett djur som heter" + name);
    System.out.println();
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}
    ----------------------------------------------------------
package animals;

public class Cat extends Animal {

public Cat (String name) {
    setName(name);
}

public void introduceYourself(){
System.out.println("Mjau. I'm a cat called " + getName());
}
}

Errorcode: Implicit super constructor Animal() is undefined. Must explicitly invoke another constructor

zkalman
  • 17
  • 6

3 Answers3

0

The problem lies in that the constructor in Cat is attempting by default to call on the default constructor for Animal which by defining your constructor in Animal you have removed. Constructors of classes that extend some other class will automatically call on the default constructor of their superclass even if you don't explicitly write it in.

So your code should look something like this

public class Animal {
   public Animal(String string){
   }
   //other code in the class
}

public class Cat{
   public Cat(String name){
      super(name);
   }
   //other code in the class
}

The super(name) call here calls on the constructor we have defined for Animal as the super refers to the class than Cat extends from.

Maltanis
  • 513
  • 1
  • 5
  • 13
0

So you defined a class hierarchy where a Cat inherits Animal. What Java does when you create a Cat is that it creates a new instance of Cat. What Java actually does under the hood is:

  1. It calls the Cat constructor (which you have provided)
  2. Because Cat inherits from Animal it then also wants to call the Animal constructor. By default it calls the parameterless constructor. However, you have provided only a constructor for Animal that accepts a String. That's why it's complaining.

To fix it you can either provide a parameterless constructor

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

public class Cat extends Animal {
    public Cat(String name) {
        super();
        setName(name);
    }
}

Or you can call the Animal constructor and pass it a name directly

public class Animal {
    private String name;
    public Animal(String name){
        this.name = name;
    }
}

public class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }
}
FarwoodHill
  • 552
  • 1
  • 9
  • 18
0

This is Error is caused by the constructor of your subclass implicitly calling on the default constructor of your base class

Your code is actually doing this:

public class Cat{
   public Cat(String name){
      super();
      setName(name);
   }
   //other code in the class
}

Because you have defined a constructor in your base class, Animal, the default constructor no longer existences. So you need can do one of two things:

Add a default constructor that does nothing (or does helps you build your object without the need of passing in any arguments

  public class Animal {
    String name;
    public Animal(){
    }
    public Animal(String string) {
    }
/// other code
}

Or call the base class constructor you did create and modify your subclass constructor to do whatever extra is needed for that subclass.

public class Animal {
    private String name;
    public Animal(String name){
        this.name = name;
    }
///other code
}

public class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }
/// other code
}

Something you should think about, when creating subclasses, is the usefulness of inheritance. Having a base class that can do general actions and setup makes it so that subclasses should only do what's unique to them.

If a base class is being used as a label instead of a shared functionality, then it might be best to make the base class an interface. When it comes to constructors you should have any general setup be done in the base class constructor and then set fields that are unique to the subclass in the subclass constructor.

public class Animal {
    private String name;
    private int legs;
    public Animal(String name, int num_of_legs){
        this.name = name;
        this.legs = num_of_legs;
    }
    protected int getAnimalLegs(){
        return this.legs;
    }
/// other code, getters, setters, etc.
}

public class Cat extends Animal {
    private int cuteness;
    public Cat(String name, int num_of_legs, int level_of_cuteness) {
        super(name, num_of_legs);
        this.cuteness = level_of_cuteness;
    }
/// other code
}

This would set the private variables name and legs on a super class of Cat and make the constructor for Cat only perform actions that relate it's fields i.e. setting the level of cuteness for the Cat object. Also note that you can't call code before you call the super constructor in your subclass constructor. If you do, you will end up with another error.

Thatalent
  • 404
  • 2
  • 8