Roselyn
Roselyn

Reputation: 23

Java error-actual and formal argument lists differ in length

I have written the following Java Program. But I keep getting an error saying that my constructor's actual and formal arguments differ in length. I do not understand what the problem is. Can someone please tell me what is wrong with my program and how I can debug it?

class Student 
{
 String name;
 Student(String name)
 {
  System.out.println("\nThe name of the student is:"+name);
 }
}
class Exam extends Student 
{
 int m1, m2;
 Exam(int m1, int m2)
 {
  System.out.println("\nThe marks obtained in first subject is:"+m1);
  System.out.println("\nThe marks obtained in second subject is:"+m2);
 }
}
class Result extends Exam 
{
 int totalmarks;
 int compute(int m1, int m2)
 {
  totalmarks=m1+m2;
  return(totalmarks);
 }
}
public class StudentTest 
{
 public static void main(String[] args)
 {
  Student s = new Student("rose");
  Exam e = new Exam(20,20);
  Result r = new Result();
  System.out.println("\nThe total marks obtained is:"+r.compute(20,20));
 }
}

Upvotes: 2

Views: 825

Answers (3)

Anderson Vieira
Anderson Vieira

Reputation: 9049

To solve your problem, you should remove the unnecessary inheritances:

// remove extends Student
class Exam {
    // class code here
} 

// remove extends Exam
class Result {
    // class code here
}

Given your classes, I don't see a reason for Exam to extend Student and Result to extend Exam.

Think about extends as a "is a" relation. You should extend if you can say that Exam is a Student and that Result is a Exam. It makes no sense in this context.

When a class extends another, and the subclass constructor is called, the first thing it needs to do is to call the superclass constructor. If you do not call it yourself, it will get called implicitly. The problem is that it tries to call the default constructor, but the default constructor is only created if you don't create your own.

So when you do:

new Exam(20, 20)

It does really:

Exam(int m1, int m2) {
    super(); // implicit call to Student default constructor
    System.out.println("\nThe marks obtained in first subject is:"+m1);
    System.out.println("\nThe marks obtained in second subject is:"+m2);
}

It is trying to call the default parameterless Student() constructor. But since you created Student(String name), the default does not exist. That's why you get the error.

Upvotes: 2

Touchstone
Touchstone

Reputation: 5972

Define non parameterized constructor in Student and Exam

In Student class:

Student(){
    //Give your implementation
}

In Exam class:

Exam(){
    //Give your implementation
}

Upvotes: 0

Stultuske
Stultuske

Reputation: 9437

Your problem is your hierarchy. The first step during the execution of a constructor, is to run the default constructor of the parent class.

Exam extends Student, but student doesn't have a default constructor. Add one.

Better yet, re-think your logic. Exam extending Student makes no sense at all. An exam is taken by a student. You are saying that an exam IS a student.

Upvotes: 1

Related Questions