wloleo
wloleo

Reputation: 2210

Is it inheritance that make my compilation error?

Why the code is not compiling? so what is the reason why it is not compiling can someone state or elaborate it for me?

class Pet{
    public Pet(int age){
        System.out.print("Pet age: " + age);
    }
}
public class Cat extends Pet{
    public Cat(){
        System.out.print("Cat");
    }
    public static void main(String[] args) {
        new Pet(5);
    }
}

Error Log:

Cat.java:7: error: constructor Pet in class Pet cannot be applied to given types;
    public Cat(){
                ^
  required: int
  found:    no arguments
  reason: actual and formal argument lists differ in length
1 error

Upvotes: 0

Views: 545

Answers (7)

user14377369
user14377369

Reputation:

you have to call super class with an integer parameter. when main method call new object it calls Cat constructor it will search super class no arg constructor. but there is no no arg constructor so either need to added super class default constructor or call the super with parameter. sample code is below.

    class Pet{
    public Pet(int age){
        System.out.print("Pet age: " + age)}}
public class Cat extends Pet{
    public Cat(){
        super(5);
        System.out.print("Cat");}
    public static void main(String[] args) {
        new Pet(5);}}
 

Upvotes: 1

Error Hunter
Error Hunter

Reputation: 1684

Default constructor missing in Pet.since you are extending Pet in Cat you need a super constructor to avoid this

class Pet{
   public Pet(){};
    public Pet(int age){
        System.out.print("Pet age: " + age);
    }
}
public class Cat extends Pet{
    public Cat(){
        System.out.print("Cat");
    }
    public static void main(String[] args) {
      
        new Pet(5);

    }
}

Upvotes: 1

Keyur Mahajan
Keyur Mahajan

Reputation: 134

In Cat constructor parent constructor(Pet) will be called. So you either you need to defined default no argument constructor in Pet class or call super(5) in Cat default constructor.

Upvotes: 1

Gaurav Jeswani
Gaurav Jeswani

Reputation: 4592

When call is made to the Cat() constructor of Cat class, it will internally call the default constructor of parent class Pet. While when you declared the parameterized constructor for Pet class, it has no more default constructor exist there.

In order to fix it, you need to explicitly declare the default constructor for parent class Pet. Or explicitly call parameterized constructor of parent class.

Upvotes: 1

Anand Vaidya
Anand Vaidya

Reputation: 1461

The error clearly says that you dont have default constructor in your parent class.

Every Java class has an empty constructor by default. but when you create a Parameterised constructor by yourself, It replaces the empty constructor.

To fix the error, either create an empty constructor in your Parent class, Or Call the parameterised constructor from your parent class.

This is the modified version of your code that should work.

class Pet{
public Pet(int age){
    System.out.print("Pet age: " + age);
}
}
public class Cat extends Pet{
    public Cat(int age){
        super(age);
        System.out.print("Cat");
    }
    public static void main(String[] args) {
        new Cat(5);
    }
}

Upvotes: 1

Sami Farhat
Sami Farhat

Reputation: 1162

Your Cat class inherits from the Pet class.

Pet class requires an integer in its default constructor.

Therefore the default constructor of Cat must call the Pet constructor with an integer parameter.

Alternatively, you can define a default constructor in Pet which does not take in an integer.

You should be able to call the parent constructor as such:

public class Cat extends Pet{
    public Cat(){
        super(3); 
        System.out.print("Cat");
    }
}

Upvotes: 1

Henry
Henry

Reputation: 43798

The Cat constructor needs to call the constructor of the superclass Pet with an age argument.

For example something like this:

public Cat(int age){
    super(age)
    System.out.print("Cat");
}

Upvotes: 2

Related Questions