Question

It's my first time posting here on Stack Overflow, but I'm having trouble with an early step of a project I am working on (written in Java) and am looking for guidance. I've searched the forums, but am still having a difficult time understanding the error I'm making.

I'm creating a juvenile email-like system and, more specifically, working on class hierarchy and inheritance. My superclass is Attachments and a subclass of this is ContainsDescription.

When compiling, the error is occurring in the constructor of the ContainsDescription subclass. The error reads, "Actual and formal argument lists differ in length," which I don't quite know how to interpret. My code reads as follows:

public class Attachments
{
    private String filename;
    private int size;

    public Attachments(String filename, int kilobytes)
    {
        this.filename = filename;
        size = kilobytes;
    }

    public void preview()
    {
        System.out.println("Filename: " + filename + "\n");
        System.out.println("Size: " + size + " kB" + "\n");
    }
}

public class ContainsDescription extends Attachments
{
    private String description;

    public ContainsDescription(String description)
    {
        this.description = description;
    }
}

Some direction would be hugely appreciated, as much of my project consists of tasks like this one.

Was it helpful?

Solution

You have to provide a contructor in ContainsDescription that takes the parameters of your superclass.

You have to combine them:

public class ContainsDescription extends Attachments
{
    private String description;

    public ContainsDescription(String description, String filename, int kilobytes)
    {
        super(filename, kilobytes);
        this.description = description;          
    }
}

If you don't, it will not be able to construct the superclass. If you really only want a constructor that takes a Description as argument you can provide default values:

public class ContainsDescription extends Attachments
{
    private String description;

    public ContainsDescription(String description)
    {
        super("", 0);
        this.description = description;          
    }
}

OTHER TIPS

What happening here is called constructor chaining in java , You can read more here

Since your parent class has a constructor with arguments , you need to explicitly invoke that. Otherwise there will be an implicit invoke to the default constructor without arguments, In your case default constructor is not available. If you havent invoked it your code is same as

        public class ContainsDescription extends Attachments
    {
        private String description;

        public ContainsDescription(String description)
        {

            super();    //implicitly added by java. This will look for constructor withod argument in the super class
            this.description = description;
        }
    }

You are getting the "Actual and formal argument lists differ in length" error simply because Java calls the default constructor of your superclass without any parameters.

However, your superclass doesn't have a constructor that accepts an empty parameter list. This would lead to the error of "Actual and formal argument lists differ in length", as its trying to construct with an empty parameter list, but all the constructors you have provided take in parameter lists of different sizes.

There are two things you can do.

  1. Either, create another constructor of your superclass that accepts no parameters.
  2. Or, make a call to super() in your inherited constructor, with the appropriate parameters.

This feature of OOP is essential in understanding the flexibility involved. This feature allows you to customize the state of the object defined by the super class before (or after) making the specific changes to such object once in the scope of the subclass. Such chaining of constructors among a chain of inheritances is a very powerful aspect of Java and OOP in general.

Write a constructor

public Attachments(){}

in your Attachments class so that your inheritance class can call the super class's constructor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top