Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Extending Classes

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?

like image 799
Sameer Anand Avatar asked Oct 21 '13 18:10

Sameer Anand


People also ask

What is extending classes in Java?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

Can I extend 2 classes in Java?

You can't extend two or more classes at one time. Multiple inheritance is not allowed in java.

Why would you extend a class in Java?

You extend a class when you want the new class to have all the same features of the original, and something more. The child class may then either add new functionalities, or override some funcionalities of the parent class.

Can you extend a regular class in Java?

You can extend any class as long as its not final.


1 Answers

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;
    }
}
like image 74
flavian Avatar answered Sep 26 '22 12:09

flavian