Home > database >  Error when extending from abstract parent class
Error when extending from abstract parent class

Time:12-18

Error

public Cat (String nm, int legs, String sd, String col)

For this constructor I got following compiler error:

constructor Animal in class Animal cannot be applied to given types;
required: String, int
found: no arguments
reason: actual and formal arguments lists differ in length

Code

The parent class is right below the child class.

public class Cat extends Animal {
    
    private String sound;
    private String colour;
    
    public Cat (String nm, int legs, String sd, String col) {
        
        nm = super.getName();
        legs = super.getNumOfLegs();
        sound = sd;
        colour = col;
    }
    
    public abstract class Animal {
    
        protected String name;
        protected int numOfLegs;
        
        public Animal() {    
        }

        public Animal(String nm, int legs) {
            name = nm;
            numOfLegs = legs;      
        }

        public String getName() {
            return name;
        }

        public int getNumOfLegs() {
            return numOfLegs;
        }
    
        public abstract String display();
    }
}

Should the parent abstract class be placed in a separate file instead?

I've tried that initially but it returned way more errors than it did now, especially from the abstract method display().

What is causing the error?

CodePudding user response:

There are a couple of things you should change.

First of all, it is the best way to put the super class into a separate file. If you want to keep in one file you need drag it out of the Cat class and remove the scope (not public or private). But this is not a good coding style for a super class.

The next thing is, with the name/nm and legs/numOfLegs. Either you call the super constructor and provide the two variables (see my example) or you use name = nm; and numOfLegs = legs;

You should also reconsider if the name and numOfLegs varialbes need to be protected or if is fine to provide the access only through the getter.

If the number of legs, the name, sound and color will not change you could also make them immutable (with the key word final, e.g. private final String sound). If not you can make them accessible with a setter.

Finally you need to implement the abstract method in the Cat class...

public class Cat extends Animal {

    private String sound;
    private String colour;

    public Cat(String nm, int legs, String sd, String col) {
        super(nm, legs);
        sound = sd;
        colour = col;
    }

    @Override
    public String display() {
        return null;
    }
}

abstract class Animal {

    protected String name;
    protected int numOfLegs;

    public Animal(String nm, int legs) {
        name = nm;
        numOfLegs = legs;
    }

    public String getName() {
        return name;
    }

    public int getNumOfLegs() {
        return numOfLegs;
    }

    public abstract String display();
    
}
  • Related