OiO.lk Blog java Constructor in class cannot be applied to given types …. no arguments reason: actual and formal argument lists differ in length
java

Constructor in class cannot be applied to given types …. no arguments reason: actual and formal argument lists differ in length


Assignment:

Develop a public subclass named Dog that extends an existing class called Animal. The Animal class models an animal with basic attributes such as name (String) and age (int), with corresponding public getters and setters:

public class Animal {
    private String name;
    private int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

In the Dog subclass:

include a constructor Dog(String name, int age). Assume the Animal class has a constructor Animal(String name, int age);
introduce a new public method named bark() that returns the String "Woof!".

My Code:

public class Dog extends Animal{

    public Dog(String name, int age){
        setName(name);
        setAge(age);
    }

    public Dog(){}
    
    public String bark(){
        return("Woof!");
    }
}

The error:

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

Screenshot of error enclosed.

I did it exactly like the example in my Java text. What am I missing here? Thanks in advance!



You need to sign in to view this answers

Exit mobile version