0

I have a class called SeatingPlan which inherits from Seat.

In the SeatingPlan constructor I'm initializing as:

public SeatingPlan(int numberOfRows, int numberOfColumns) {
   super();
   rows = numberOfRows;
   columns = numberOfColumns;
   seats = new Seat[rows][columns];  // (Seat [][] seats during variable declarations)
}

Seat.java:

public Seat(String subject, int number) {
   courseSubject = subject;
   courseNumber = number;
}

However I'm getting this error:

SeatingPlan.java:8: error: 
    constructor Seat in class Seat cannot be applied to given types;
        super();
        ^
      required: String,int
      found: no arguments
      reason: actual and formal argument lists differ in length
    1 error
    [ERROR] did not compile; check the compiler stack trace field for more info
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • 1
    Your superclass has only a constructor with two args (the one you put above). Add a default no-args constructor. – Slimu Oct 20 '20 at 13:58
  • 1
    You could call `super` with a subject and number, but I think it would make more sense just to not have `SeatingPlan` inherit from `Seat` – user Oct 20 '20 at 14:01
  • 4
    Rather step back. A seating plan isnt a seat, and a seat isnt a seating plan. So why exactly do you want to couple them via inheritance?! Meaning: you use inheritance on *purpose*. To create *helpful* abstractions to model reality in ways that enable you to write code to implement your requirements. Making a SeatPlan a Seat ... somehow doesnt fit into that idea. – GhostCat Oct 20 '20 at 14:01
  • 2
    Does this answer your question? [implicit super constructor Person() is undefined. Must explicitly invoke another constructor?](https://stackoverflow.com/questions/23395513/implicit-super-constructor-person-is-undefined-must-explicitly-invoke-another) – MDK Oct 20 '20 at 14:03
  • 2
    Sounds to me based on your object names like a `SeatingPlan` should organize and manage a collection of `Seat` objects, not inherit from it. Might want to instead add an [ArrayList](https://www.geeksforgeeks.org/arraylist-in-java/) of `Seat` objects to your `SeatingPlan` and then build in functionality to fill-in and interact with that `ArrayList` instead. – Tim Hunter Oct 20 '20 at 14:11
  • 1
    Are you sure that a *SeatingPlan* **is-a** *Seat*? Because inheritance only makes sense if you have a true is-a relationship. – Polygnome Oct 20 '20 at 15:12
  • 3
    *" have a class called Seat and SeatingPlan which inherits from Seat"* ... I would only like to suggest that you may wish to think of a different composition. Inheritance is *best* used when there is an *is a* relationship between child and parent classes. In your case, a `SeatingPlan` is **not** a `Seat` ... so it seems that this may not be a good use case for inheritance. – scottb Oct 20 '20 at 15:24

3 Answers3

0

You are calling super() while you don't have a default constructor that does not accept parameters. So, add the below constructor and it will work. Or add the required parameters in the super(param, param) call.

public Seat() {
}
Aman
  • 1,627
  • 13
  • 19
0

Either you need a default empty constructor for Seat or you need to call super with the arguments super(subject, number)

Aditya Patil
  • 548
  • 4
  • 14
0

Problem is, in Java when you overload the constructor default constructor won't be provided by the compiler automatically anymore. So, if you still need to make use of it then you need to define it in your class.

public class Seat{

    public Seat(){//Implement the no-arg constructor in your class


    }

    public Seat(String subject, int number) {
       courseSubject = subject;
       courseNumber = number;
    }

}

Now you can access the no-args constructor of parent class Seat through the SeatingPlan child class.

public SeatingPlan(int numberOfRows, int numberOfColumns) {
   super();//Now you can access the no-args constructor of Seat parent class
   rows = numberOfRows;
   columns = numberOfColumns;
   seats = new Seat[rows][columns];  // (Seat [][] seats during variable declarations)
}

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Hasindu Dahanayake
  • 1,344
  • 2
  • 14
  • 37