Reputation: 323
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?
Upvotes: 6
Views: 26755
Reputation: 15428
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
Upvotes: 2
Reputation: 3608
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)
Upvotes: 1
Reputation: 28511
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;
}
}
Upvotes: 7
Reputation: 53565
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(){}
Upvotes: 5
Reputation: 526
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
Upvotes: 0
Reputation: 9488
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()
Upvotes: 1
Reputation: 62072
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.
Upvotes: 1