6

I have a Meeting class and I want to create a BusinessMeeting class which extends Meeting.

This is my Meeting class:

public class Meeting {

private String name;
private String location;
private int start, stop;

public Meeting(String name, String location, int start, int stop) {

    this.name = name;

    this.location = location;

    this.start = start;

    this.stop = stop;

}

This is my BusinessMeeting class:

public class BusinessMeeting extends Meeting {

    public BusinessMeeting() {

        super();

    }

}

In my BusinessMeeting class, I am getting the error:

constructor Meeting in class Meeting cannot be applied to given types; required: String,String,int,int found: no arguments

I am not really sure why I am getting that error message. Shouldn't the BusinessMeeting class inherit all of the variables from my Meeting class?

Sameer Anand
  • 311
  • 1
  • 4
  • 11

7 Answers7

7
public class Meeting {
    public Meeting(String name, String location, int start, int stop) {

        this.name = name;

        this.location = location;

        this.start = start;

        this.stop = stop;

    }
}

Now say you want to extend your BusinessMeeting class with a businessId.

public class BusinessMeeting {
    private Integer businessId;

    public BusinessMeeting(String name, String location, int start, int stop, int business) {
        // because super calls one of the super class constructors(you can overload constructors), you need to pass the parameters required.
        super(name, location, start, stop);
        businessId = business;
    }
}
flavian
  • 28,161
  • 11
  • 65
  • 105
5

The constructor you created accepts 4 arguments and you call it without any. If you want it to be initialized without parameters as well - add an empty constructor to Meeting:

 public Meeting(){}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • A default constructor is not created if you define a constructor. – Scott Oct 21 '13 at 18:09
  • Adding an empty constructor to `BusinessMeeting` won't help when he calls `super();` to a super class that lacks a 0-argument constructor. – nhgrif Oct 21 '13 at 18:11
2

You are calling super class constructor with empty argument:

public BusinessMeeting() {
        super();   
    }

But your super class has the constructor: Meeting(String, String, int, int).

With super(), the superclass no-argument constructor is called(If it exists). With super(parameter list), the superclass constructor with a matching parameter list is called. So either you have to add an constructor with empty argument to Meeting e.g., public Meeting(){} or you need to call super(String, String, int, int)

public class Meeting {
// your variable declaration
   public Meeting(String name, String location, int start, int stop) {
    // your assignment 
   }
   public Meeting(){}
}

public class BusinessMeeting extends Meeting {
    public BusinessMeeting()
    {
      // super(); un-comment if you need to check by commenting below
       super("name", "loc", 1, -1);
    }
}

Check out: Using the Keyword super

Sage
  • 15,290
  • 3
  • 33
  • 38
1

By declaring a constructor, your Meeting class does not have a 0 argument constructor, which is what you're trying to call with super();. You need to call a super constructor that matches the available constructors in your super class.

To fix this, add a 0-argument constructor to Meeting class, or call the 4-argument constructor you already have.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
1

As alfasin stated, you have created a constructor. The standard states that this constructor is created if no other constructor is defined. You must create a parameterless constructor yourself if you would like to call super()

Scott
  • 9,458
  • 7
  • 54
  • 81
1

Yes and no. It depends on how you declare the variables.

Private variables in the super class are not accessible by the subclass. Usually you add protected getter and setter-Methods to the superclass. Those methods can be used in the subclass to access the variables. (a sometimes sloppy way would be to declare the variables themselve as protected)

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. Java Inheritance Webpage

But concerning the error you mentioned: You are calling super(). This means you are calling the default constructor of the superclass. As you have written your own constructor in the superclass, there is no default constructor. Latter one is only created when you do not define a constructor.

Hence, when calling the constructor of the superclass - which takes in your case four arguments - you have to pass these arguments via super() like this:

super("a name", "a location", 0, 1)

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
0

You should add constructor with empty parameters in meeting class or you can call super(name,location,start,stop) instead of super() in BussinessMeeting class

Zapateus
  • 516
  • 3
  • 8
  • 21